'API OAuth2 with VBA, Error: Missing grant type

I'm trying to receive an access token from the api of a partner via Oauth2.0. When I use CURL I get the token but not in my VBA-script. I get this error:

{"error":"invalid_request","error_description":"Missing grant type"}

The succesful CURL way is this:

curl -X POST -H "client_id:xxx" -u xxx:xxx"https://my.arrow.com/api/security/oauth/token" -d "grant_type=client_credentials"

I tried to send grant_type=client_credentials in different positions and spellings (in Body, as SetRequestHeader, grant_type=client_credentials or grant_type:client_credentials or grant_type, client_credentials) nothing helped, always same error.

This is the documentation for the API: https://my.arrow.com/more/developers/#section/Access-Token

Public Function API_MyArrow_Artikel(artikelBez) As String
Dim objHTTP As Object
Dim strHead As String, strClientId As String, strClientSecret As String, strBody As String
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
strClientId = "xxx"
strClientSecret = "xxx"
strUrl1 = "https://my.arrow.com/api/security/oauth/token"
strHead = strClientId & ":" & strClientSecret
lngADRNR = 674
objHTTP.Open "POST", strUrl1, False
objHTTP.SetRequestHeader "application", "x-www-form-urlencoded"
objHTTP.SetRequestHeader "client_id", strClientId
objHTTP.SetRequestHeader "client_secret", strClientSecret
objHTTP.SetRequestHeader "Authorization", "Basic " + Base64Encode(strHead)
'objHTTP.setRequestHeader "grant_type", "client_credentials"
objHTTP.SetTimeouts 10000, 10000, 10000, 10000 'Timeout (in milliseconds) to wait for timeout in each request phase (Resolve, Connect, Send, Receive)
objHTTP.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"

objHTTP.Send "grant_type=client_credentials" 
  
API_MyArrow_Artikel = objHTTP.responseText
End Function


Solution 1:[1]

The problem got solved. The correct way to use grant_type is as follows:

strUrl1 = "https://my.arrow.com/api/security/oauth/token?grant_type=client_credentials"

grant_type not in header or something else will work. Best is add to link.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dada