首页 > 解决方案 > vba 中的 MSXML2.XMLHTTP60 对象不起作用

问题描述

我编写了一个程序来获取令牌以与 Microsoft Access 中的 PayPal API 一起使用。

但是 xmlhttp.send "grant_type = client_credentials") 行无法正常工作。有时我会收到两种类型的错误消息,有时它可以正常工作。

请帮我解决这个程序,哦,告诉我这个错误的原因!

Dim xmlhttp As New MSXML2.XMLHTTP60

Dim tempStr As String
Dim requestUrl_2 As String

Dim requestMethod As String

Dim encodeData As String

requestUrl_2 = "https://api.sandbox.paypal.com/v1/oauth2/token"

requestMethod = "POST"
encodeData = Base64EncodeString(ClientID & ":" & ClientSecret)

xmlhttp.Open requestMethod, requestUrl_2, False

xmlhttp.setRequestHeader "Accept", "application/json"
xmlhttp.setRequestHeader "Accept-Language", "en_US"
xmlhttp.setRequestHeader "Authorization", "Basic " & encodeData

xmlhttp.send "grant_type=client_credentials"
' ------------------------------------------------------------------------------------------------------

If xmlhttp.status = 200 Then
    tempStr = Split(Split(xmlhttp.responseText, "access_token")(1), "token_type")(0)
    
    tempStr = Left(tempStr, Len(tempStr) - 3)
    
    tempStr = Right(tempStr, Len(tempStr) - 3)
    
    MsgBox tempStr
Else
    MsgBox "Error! Response status returned " & xmlhttp.status
End If

Set xmlhttp = Nothing

当我第一次运行应用程序并调用此过程时,我在 xmlhttp.send "grant_type = client_credentials") 行收到错误):“指定资源的下载失败。”

之后,我将错误行的代码更改为“xmlhttp.send”,然后出现错误。xmlhttp.responseText: {"error": "unsupported_grant_type", "error_description": "Grant Type is NULL"}

之后,我再次使用 xmlhttp.send "grant_type = client_credentials") 行运行该过程 - 并且没有更多错误。我可以无休止地使用这个程序(在我重新启动我的应用程序之前)

标签: vbams-accesspaypal

解决方案


推荐阅读