使用VBA调用Sendpulse API授权接口遇「unsupported_grant_type」错误
解决VBA调用Sendpulse API授权报错问题
问题根源
你的代码中POST请求的参数格式错误:x-www-form-urlencoded类型的请求需要用&分隔参数,而你用了逗号加空格,导致服务器无法正确识别grant_type参数,因此返回unsupported_grant_type错误。
修正后的代码
Dim URL As String Dim xmlHttp As Object Dim data As String Set xmlHttp = CreateObject("MSXML2.ServerXMLHTTP.6.0") '推荐使用指定版本,兼容性更好 URL = "https://api.sendpulse.com/oauth/access_token" ' 用&分隔参数,删除多余的逗号和空格,同时检查client_secret末尾的多余符号 data = "grant_type=client_credentials&client_id=-redacted-&client_secret=-redacted-" xmlHttp.Open "POST", URL, False xmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" xmlHttp.send data Debug.Print xmlHttp.ResponseText
额外检查点
- 确认
client_id和client_secret没有多余的符号(比如你原代码中client_secret结尾多了一个-,需要移除) - 如果仍有问题,尝试启用TLS 1.2支持,部分API要求强制使用该协议,可以在
send前添加:xmlHttp.setOption 2, 13056 '启用TLS 1.2
内容的提问来源于stack exchange,提问作者Ernesto
相关产品推荐
相关产品推荐

