admin管理员组文章数量:1429791
new to this side of things and can't seem to find the proper syntax. In the POST request payload, I need to include "grant_type=client_credentials" according to the service I'm trying to access. However, I can't find the JavaScript syntax to include the username and password in the request payload. Here is what I have so far (I have removed my key and secret for obvious reasons):
var request = new XMLHttpRequest();
var path=".1/auth/accesstoken";
request.onreadystatechange=state_change;
var encodedData = window.btoa(Key:Secret"); //encode consumer key and secret key
document.write(encodedData);
request.open("POST", path, true);
request.setRequestHeader("Authorization", encodedData);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.send(null);
function state_change() {
if (request.readyState == "4") //Request finished and response is ready
{
if (request.status == "200") {
alert("Success");
}
else {
alert("Problem retrieving data");
console.log(this.responseXML);
}
}
}
Thanks in advance, been stuck on this for the last few hours.
new to this side of things and can't seem to find the proper syntax. In the POST request payload, I need to include "grant_type=client_credentials" according to the service I'm trying to access. However, I can't find the JavaScript syntax to include the username and password in the request payload. Here is what I have so far (I have removed my key and secret for obvious reasons):
var request = new XMLHttpRequest();
var path="https://ops.epo/3.1/auth/accesstoken";
request.onreadystatechange=state_change;
var encodedData = window.btoa(Key:Secret"); //encode consumer key and secret key
document.write(encodedData);
request.open("POST", path, true);
request.setRequestHeader("Authorization", encodedData);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.send(null);
function state_change() {
if (request.readyState == "4") //Request finished and response is ready
{
if (request.status == "200") {
alert("Success");
}
else {
alert("Problem retrieving data");
console.log(this.responseXML);
}
}
}
Thanks in advance, been stuck on this for the last few hours.
Share Improve this question edited Apr 13, 2016 at 0:39 TechJunkie asked Apr 12, 2016 at 23:04 TechJunkieTechJunkie 1331 gold badge1 silver badge9 bronze badges1 Answer
Reset to default 2Just needed to remove null from the send payload and replace with 'grant_type=client_credentials'.
var request = new XMLHttpRequest();
var path="https://ops.epo/3.1/auth/accesstoken";
request.onreadystatechange=state_change;
var encodedData = window.btoa("Key:secret"); //encode consumer key and secret key
request.open("POST", path, true);
request.setRequestHeader("Authorization", encodedData);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.send('grant_type=client_credentials');
function state_change() {
if (request.readyState == "4") //Request finished and response is ready
{
if (request.status == "200") {
alert("Success");
document.write(request.responseText);;
}
else {
alert("Problem retrieving data");
console.log(this.responseXML);
}
}
}
本文标签: javascriptXMLHTTPRequest Returning Bad Request (400)quotRequire Param granttypequotStack Overflow
版权声明:本文标题:javascript - XMLHTTPRequest Returning Bad Request (400) - "Require Param: grant_type" - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745460158a2659287.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论