2017-08-03 26 views
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发出相同的请求时,我得到了端点的正确响应。请帮我调试一下。

回答

0

我加入了'Content-Length'头中的restService预期如下

​​

现在它的工作。想知道为什么login.microsoftonline.com会返回缺少标题的404