0
我試圖使用this文檔爲我的應用程序實現OneDrive連接。我從參數中獲得了code
,並試圖使用文檔中的代碼流的第2步檢索access_token
。我的服務器是一個NodeJS應用程序。Onedrive oauth,代碼流程步驟2不起作用
我有一個名爲restService
的自定義服務,用於從服務器發出REST請求。下面是它
const https = require('https');
let makeRequest = (host, endPoint, method, requestOptions={}) => {
return new Promise ((resolve, reject) =>{
var options = {
host: host,
port: 443,
path: endPoint,
method: method,
headers : requestOptions.headers || {}
};
var req = https.request(options, function(response) {
let body = "";
response.on('data', (d) => {
body += d;
});
response.on('end', function() {
resolve({
statusCode : response.statusCode,
body : body
})
});
response.on('error', function() {
reject("Error while making request");
});
});
if(!!requestOptions.body){
req.write(requestOptions.body);
}
req.on('error', (e) => {
console.error("ERROR WHILE MAKING API call to " + host + " :", e)
reject(e);
});
req.end();
})
}
使用上述服務的代碼,我提出一個請求來檢索的access_token如下
let endpoint = "/common/oauth2/v2.0/token";
let body = "grant_type=authorization_code&client_id="+config.onedrive.client_id
+"&redirect_uri="+encodeURIComponent(config.onedrive.redirect_uri)
+"&client_secret="+config.onedrive.client_secret+"&code="+code;
let requestOptions = {
headers : {
"Content-Type" : "application/x-www-form-urlencoded"
} ,
body : body
}
restService.makeRequest("login.microsoftonline.com", endpoint, "POST", requestOptions)
.then(data=>{console.log(data)})
但隨着代碼流的第2步中提到,無論我做什麼,我收到了響應
{
statusCode : 404
body : ""
}
from restService.makeRequest()。但是,當從POSTMAN發出相同的請求時,我得到了端點的正確響應。請幫我調試一下。