从网站上获取Web应用程序的访问令牌时遇到一些问题。对以下内容的回应是
"{"error":"invalid_request","error_description":"The grant type was not specified in the request"}"
。
我已在下面指定了授权类型,但似乎我没有正确格式化请求。
有什么建议吗?
var getToken = new XMLHttpRequest();
getToken.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML =
this.responseText;
}
};
getToken.open("POST", "https://api2.libcal.com/1.1/oauth/token", true);
getToken.send('grant_type=client_credentials','client_id=XXX', 'client_secret=XXXXXXXXXXXXXXXXXXXX');
答案 0 :(得分:0)
当您正在进行Post Request以获取访问令牌时,参数应该在正文中发送(JSON),如下所示:(我测试过,它工作正常)
// form data for the post request
var data = {
"grant_type":"client_credentials",
"client_id": "XXX",
"client_secret": "XXXXXXXXXXXXXXXXXXXX"
};
// construct an HTTP request
var getToken= new XMLHttpRequest();
getToken.open("POST", "https://api2.libcal.com/1.1/oauth/token", true);
getToken.setRequestHeader('Content-Type', 'application/json');
// send the collected data as JSON
getToken.send(JSON.stringify(data));