I'm trying to request an access token with vba code. When putting all the parameters I give in VBA into postman I get an access token no problem but when using it in VBA code it gives the error
"the request body must contain the following parameter: 'grant_type'"
Dim objRequestGetToken As Object, JSONGetToken As Object
Dim strUrlGetToken As String, strClientID As String, strClientSecret As String, strResource As String, strBody As String
Dim blnAsyncGetToken As Boolean
strClientID = "XXX"
strClientSecret = "XXX"
strRecourse = "00000003-0000-0ff1-ce00-000000000000/XXX.sharepoint.com@XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
strBody = "{""grant_type"":""client_credentials"",""client_id"":""" & strClientID & """,""client_secret"":""" & strClientSecret & """,""resource"":""" & strRecourse & """}"
Set objRequestGetToken = CreateObject("MSXML2.XMLHTTP.6.0")
strUrlGetToken = "https://accounts.accesscontrol.windows.net/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/tokens/OAuth/2/"
blnAsyncGetToken = False
Debug.Print strBody
With objRequestGetToken
.Open "POST", strUrlGetToken, blnAsyncGetToken
.SetRequestHeader "Content-Type", "application/json;odata=verbose"
.SetRequestHeader "Accept", "application/json;odata=verbose"
.send strBody
'spin wheels whilst waiting for response
While objRequestGetToken.readyState <> 4
DoEvents
Wend
Set JSONGetData = JsonConverter.ParseJson(objRequestGetToken.ResponseText)
End With
Debug.Print objRequestGetToken.ResponseText
I know this is a possible duplicate request with VBA post method request body ("MSXML2.XMLHTTP"): Error Parsing JSON: ^ Expecting '{' or '[' but I can't figure out why my body is not properly being sent and the post previously mentioned doesn't seem to help.
postman request:
Any kind of help would be much appreciated!
Edit: Here's the code that made it work eventually :)
Dim objRequestGetToken As Object, JSONGetToken As Object
Dim strUrlGetToken As String, strClientID As String, strClientSecret As String, strResource As String, strBody As String
Dim blnAsyncGetToken As Boolean
strClientID = "XXX"
strClientSecret = "XXX"
strRecourse = "00000003-0000-0ff1-ce00-000000000000/XXX.sharepoint.com@XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
strBody = "{""grant_type"":""client_credentials"",""client_id"":""" & strClientID & """,""client_secret"":""" & strClientSecret & """,""resource"":""" & strRecourse & """}"
Set objRequestGetToken = CreateObject("MSXML2.XMLHTTP.6.0")
strUrlGetToken = "https://accounts.accesscontrol.windows.net/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/tokens/OAuth/2/"
blnAsyncGetToken = False
Debug.Print strBody
With objRequestGetToken
.Open "POST", strUrlGetToken, blnAsyncGetToken
.SetRequestHeader "application", "x-www-form-urlencoded"
.SetRequestHeader "Accept", "application/json;odata=verbose"
.send strBody
'spin wheels whilst waiting for response
While objRequestGetToken.readyState <> 4
DoEvents
Wend
Set JSONGetData = JsonConverter.ParseJson(objRequestGetToken.ResponseText)
End With
Debug.Print objRequestGetToken.ResponseText
CodePudding user response:
strBody = "grant_type=client_credentials&client_id=" & strClientID & "&client_secret=" & strClientSecret & "&resource=" & strRecourse
and changing the header to x-www-form-urlencoded
With objRequestGetToken
.Open "POST", strUrlGetToken, blnAsyncGetToken
.SetRequestHeader "application", "x-www-form-urlencoded"
.SetRequestHeader "Accept", "application/json;odata=verbose"
.send strBody
'spin wheels whilst waiting for response
While objRequestGetToken.readyState <> 4
DoEvents
Wend
Set JSONGetData = JsonConverter.ParseJson(objRequestGetToken.ResponseText)
End With
should solve the issue.