在VBA中创建令牌UPS API oauth2

问题描述 投票:0回答:1

尝试将旧的 UPS API 转换为新的 oauth2.0。获取错误消息为:

{"response":{"errors":[{"code":"10400","message":"无效/缺少授权标头"}]}}

这是来自 UPS 示例代码:

curl -i -X POST \
  -u {clientID}:{clientSecret} \
  https://wwwcie.ups.com/security/v1/oauth/token \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'x-merchant-id: string' \
  -d grant_type=client_credentials
Sub POST_Method_Example()
Dim strHead As String, strClientId As String, strClientSecret As String
strClientId = "xxx"
strClientSecret = "xxx"
strHead = strClientId & ":" & strClientSecret

    With CreateObject("MSXML2.ServerXMLHTTP")
        .Open "POST", "https://wwwcie.ups.com/security/v1/oauth/token"
        .setRequestHeader "Authorization", "Basic " + EncodeBase64(strHead)
        .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        .setRequestHeader "x-merchant-id", "string"
        .send "grant_type=client_credentials"
        Debug.Print .responseText
    End With

End Sub

更新为 BASE64 编码

vba base64 token ups
1个回答
0
投票

根据 https://developer.ups.com/api/reference?loc=en_US#operation/GenerateToken

{clientID}:{clientSecret}
需要进行 Base64 编码。

请参阅(例如)https://stackoverflow.com/a/169945/478884了解 B64 编码的 VBA 方法

© www.soinside.com 2019 - 2024. All rights reserved.