我正在尝试从Excel VBA中使用RESTfull API。
我已经在C#中使用了一个工作版本:
//Request Auth Token
var client = new RestClient("https://api.xxx.com/exp/oauth2/v1/access_token_cors");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "response_type=token&grant_type=client_credentials&client_id=1234&client_secret=1234&scope=", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
需要将此代码移植到VBA。我写道:
Set MyRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
MyRequest.Open "POST", "https://api.xxx.com/exp/oauth2/v1/access_token_cors", False
MyRequest.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
PostData = """application/x-www-form-urlencoded"", ""response_type=token&grant_type=client_credentials&client_id=1234&client_secret=1234&scope="""
MyRequest.send (PostData)
当我运行VBA版本时,我收到错误“与服务器的连接异常终止”。发送行
因为它在C#中工作,所以它不能是防火墙或服务器问题。我能做些什么才能让它发挥作用?我搜索过类似的问题,但没有一个适用于我的情况。