var clientId = “4c3c9c1ab558bbe475b1e41d9f3b6dcb2e14ad45”; // Xiboで登録したOAuthクライアントのIDを指定
var clientSecret = “294a8e1dc7ee600046be438e78c660bb10648cbff9c6f7bc0619b04a18cff186ab09268743d92526157872e5f6f688a9df082fddf6de2f864c9bd0fb3f33606016a61535b22e87c005a431facd46bd21576326b56a8de5ff7f181cfdeba5b4efe7040063f25cb34717f1beeb7bbf991bd7a211e9b064bad621988bf77f457b”; // Xiboで登録したOAuthクライアントのシークレットを指定
var strUrl = “http://10.23.10.72/api/authorize/access_token”;
var baseuri = ‘http://10.23.10.72’;
var response;
function GetAccessToken() {
const xmlhttp= new XMLHttpRequest();
//リクエストの設定
xmlhttp.open('POST',strUrl,[false,clientId,clientSecret]); // clientIdとclientSecretを送信して認証
xmlhttp.setRequestHeader("Content-Type","multipart/form-data");
xmlhttp.withCredentials=true;
xmlhttp.send('grant_type=client_credentials');
if(xmlhttp.status == 200){
response = JSON.parse(xmlhttp.responseText);
console.log(response);
}
else{
// response = JSON.parse(xmlhttp.responseText);
response = xmlhttp.responseText;
console.log(response);
}
return response;
}
console.log(GetAccessToken());
The responseText is blank.
ステータス0
レディーステータス1
Besides several other issues, the Authorization headers weren’t set correctly in your original code. The Content-Type multipart/form-data
is unnecessary unless you’re uploading files. Use application/x-www-form-urlencoded instead.
You can try if this snippet works (untested):
Code
var clientId = "...";
var clientSecret = "..";
var strUrl = "http://10.23.10.72/api/authorize/access_token";
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", strUrl, true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader(
"Authorization",
"Basic " + btoa(clientId + ":" + clientSecret)
);
var body = "grant_type=client_credentials";
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState === 4) {
if (xmlhttp.status === 200) {
var response = JSON.parse(xmlhttp.responseText);
console.log("Success:", response);
} else {
console.error("Error:", xmlhttp.responseText);
}
}
};
xmlhttp.send(body);
var clientId = “4c3c9c1ab558bbe475b1e41d9f3b6dcb2e14ad45”; // Xiboで登録したOAuthクライアントのIDを指定
var clientSecret = “294a8e1dc7ee600046be438e78c660bb10648cbff9c6f7bc0619b04a18cff186ab09268743d92526157872e5f6f688a9df082fddf6de2f864c9bd0fb3f33606016a61535b22e87c005a431facd46bd21576326b56a8de5ff7f181cfdeba5b4efe7040063f25cb34717f1beeb7bbf991bd7a211e9b064bad621988bf77f457b”; // Xiboで登録したOAuthクライアントのシークレットを指定
var strUrl = “http://10.23.10.72/api/authorize/access_token”;
var response;
function GetAccessToken() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", strUrl, true);
//xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader(
"Authorization",
"Basic " + btoa(clientId + ":" + clientSecret)
);
var body = "grant_type=client_credentials";
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState === 4) {
if (xmlhttp.status === 200) {
response = JSON.parse(xmlhttp.responseText);
console.log("Success:", response);
} else {
console.error("Error:", xmlhttp.responseText);
console.error("Error:", xmlhttp.response);
console.error("Error:", xmlhttp);
}
}
};
xmlhttp.send(body);
return response;
}
console.log(GetAccessToken());
I ran the above code and got the following response:
undefined
Error:
Error:
Error: XMLHttpRequest {onreadystatechange: ƒ, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}