Missing parameter in Core.HTTPSocket body

Hi all, I am trying to use Sharepoint REST api in a Xojo web app. I have tried a few methods as shown in the HTTPSocket docs, one using a dic to json to
mem block and another using urlencoded form data. The following code is in an open event on the container where I want to manage some files. I tried to check the request using fiddler, but it is nowhere to found … I am unable to get either to work and I’m not sure why, would greatly appreciate any help troubleshooting. Thanks!

dic to json - ERROR request body must contain parameter grant_type

Dim req As New Xojo.Core.Dictionary

req.Value("grant_type") = "client_credentials"
...

Dim json As Text 
json = Xojo.Data.GenerateJSON(req)

Dim data As Xojo.Core.MemoryBlock
data = Xojo.Core.TextEncoding.UTF8.ConvertTextToData(json)

SharepointSocket1.RequestHeader("Content-Type") = "application/x-www-form-urlencoded"
SharepointSocket1.SetRequestContent(data, "application/x-www-form-urlencoded")

Dim url As Text =  "https://accounts.accesscontrol.windows.net/"+ APP.tenant_id +"/tokens/OAuth/2"

SharepointSocket1.Send("POST", url)

I’ve tried using a urlencoded form data string but it tells me credentials are incorrect, though all parameters are exactly the same as in my Postman test. I even used the old HTTPSocket, but it gives me a 302 everytime …

My Tip.
Check the request with PAW and the Xojo New Framework Generator to see best results and get the code for the Xojo…(Paw have free trial)
Postman is very generic with curl request’s…
Here some info I found about the parameter grant_type

[quote=394429:@Loannis Kolliageorgas]My Tip.
Check the request with PAW and the Xojo New Framework Generator to see best results and get the code for the Xojo…(Paw have free trial)
Postman is very generic with curl request’s…
Here some info I found about the parameter grant_type[/quote]

Thanks for the suggestion, looks good but I am working on a pc … will try to borrow a mac for a bit. Otherwise, the code looks exactly like the Xojo docs (and mine), so seems strange to be missing the param (vs just being wrong, which will give a different error).

alternative to Paw for windows is Insomnia

Did you mean Insomnia is just an alternative rest client and not that it is able to use the plugin to export as xojo code?

Got a mac and am unable to install the Xojo extension according to the instructions

Is alternative rest client but you get a very detailed response from server…

Check this post.
If you still get error please post the error you get…

From what I read about the parameter grant_type…

According to this post the oauth-2.0 parameters must be in the content of your request. Did you already try it ? This post warns also on the encoding of the body.

Something like this…

{ "uri": "https://login.windows.net/common/oauth2/token", "method": "POST", "headers": { "Content-Type": "application/x-www-form-urlencoded" }, "body": "grant_type=password&username=zzzz&password=zzz&client_id=guid&resource=https://analysis.windows.net/powerbi/api&client_secret=secret" }

[quote=394477:@Loannis Kolliageorgas]Is alternative rest client but you get a very detailed response from server…

Check this post.
If you still get error please post the error you get…

From what I read about the parameter grant_type…

[/quote]

Thanks for the assistance. I am able to make the request using the 3 different rest clients and using curl (command line). I can access the api as expected, it is only a problem in Xojo I am having. I try to make an equivalent request in Xojo by following their two form data examples in the Xojo.Net.HTTPSocket documentation, which is the same code that Paw generates, but the body is empty. I will see if I can use a terminal within the webapp and do curl, but since there is a supposedly simple built in way, I’d rather use that.

On the first post you say you get 302 (mean redirect) have you check the headers for the new return url?
Can you post here the headers response?

[quote=394584:@Loannis Kolliageorgas]On the first post you say you get 302 (mean redirect) have you check the headers for the new return url?
Can you post here the headers response?[/quote]

The 302 was only using the old HTTPSocket class, I assumed it had something to do with the protocol(http 1.0) or something. The url was the same, but the port was added (:443)

just for the sake of completeness, this was apparently an encoding issue. I tried to use the url encode method on my form data string and that didn’t help, However I noticed using Insomnia that only the slashes were encoded (as %), so I replaced them and now works as expected. Super annoying!

Make sure you’re encoding each component separately rather than encoding the entire thing once.

e.g. Do this:

Dim foo As String = EncodeURLCompenent("Name") + "=" + EncodeURLComponent("Value")

not this:

Dim foo As String = EncodeURLCompenent("Name=Value")