【问题标题】:API OAuth2 with VBA, Error: Missing grant type带有 VBA 的 API OAuth2,错误:缺少授权类型
【发布时间】:2022-01-24 18:58:21
【问题描述】:

我正在尝试通过 Oauth2.0 从合作伙伴的 api 接收访问令牌。 当我使用 CURL 时,我得到了令牌,但不在我的 VBA 脚本中。 我收到此错误:

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

成功的CURL方式是这样的:

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

我试图以不同的位置和拼写发送grant_type=client_credentials(在正文中,作为 SetRequestHeader、grant_type=client_credentials 或 grant_type:client_credentials 或 grant_type、client_credentials)没有任何帮助,总是同样的错误。

这是 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

【问题讨论】:

    标签: vba api oauth-2.0


    【解决方案1】:

    问题解决了。 grant_type的正确使用方式如下:

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

    grant_type 不在标头中,否则会起作用。最好是添加到链接。

    【讨论】:

      猜你喜欢
      • 2018-05-03
      • 1970-01-01
      • 2017-12-11
      • 2018-09-11
      • 2017-02-23
      • 1970-01-01
      • 2017-01-14
      • 2021-04-24
      • 2021-06-09
      相关资源
      最近更新 更多