Search code examples
vbabase64tokenups

Create Token UPS API oauth2 in VBA


Trying to convert the old UPS API to new oauth2.0. Getting error message as:

{"response":{"errors":[{"code":"10400","message":"Invalid/Missing Authorization Header"}]}}

This is from UPS sample code:

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

Updated with BASE64 encoding


Solution

  • According to https://developer.ups.com/api/reference?loc=en_US#operation/GenerateToken the {clientID}:{clientSecret} needs to be Base64-encoded.

    See (eg) https://stackoverflow.com/a/169945/478884 for a VBA approach to B64 encoding