首页 > 解决方案 > VBA 将 Xmlhttp 对象作为 x-www-form-urlencoded 发送

问题描述

我想将我的 Excel 与 eBay API 连接起来。对于Access Token Workflow,我需要在那里刷新我的令牌。在邮递员中,我实现了工作流程,但将请求正文转换为 VBA 时遇到问题。

eBay 需要Content Typeis application/x-www-form-urlencoded,它在请求标头中设置。如果我使用 Postman 和编码为 的正文进行 POST,x-www-form-urlencoded则可以成功访问 API。

请求标头只有 Authorization 和Content-Type. 这是 Postman 中的 Body Settings 的图像:

Postman 中的身体设置图像

如果我在这里使用原始正文执行 Postman API-POST:

grant_type=refresh_token&code=v^1.XXXXXX_HIDDEN_XXXXXFXjI2MA==&scope=https://api.ebay.com/oauth/api_scope

...然后我得到回应:

"error": "invalid_grant",
"error_description": "the provided authorization refresh token is invalid or was issued to another client"

对于 VBA,我需要发送一个原始正文(我无法解决的问题,就像我展示的那样),或者我需要将发送输入设置为x-www-form-urlencoded,但我也没有办法这样做。

这是我的 VBA 代码,它给了我一个错误,说它格式错误:

Set objHTTP = CreateObject("Microsoft.xmlhttp")
objHTTP.Open "POST", "https://api.ebay.com/identity/v1/oauth2/token", False
objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.setRequestHeader "Authorization", "Basic XXXHIDDENXXXX"
           
objHTTP.Send ("grant_type=authorization_code&code=xxHIDDENxx&scope=https://api.ebay.com/oauth/api_scope")

answer = objHTTP.responseText
MsgBox answer

这里是 VBA 错误:

{"error":"invalid_request", "error_description":"request is missing a required parameter or malformed."}

标签: vbaapipostmancontent-typeebay-api

解决方案


推荐阅读