2017-04-07 26 views
0

我想从API获取身份验证令牌。 请求应该看起来像来自Nodejs的通话

POST /oauth2/token HTTP/1.1 
Host: mysageone.ca.sageone.com 

client_id=4b64axxxxxxxxxx00710& 
client_secret=iNumzTxxxxxxxxxxhVHstrqWesH8tm9& 
code=12a0f9c12cxxxxxxxxxxxxxxx92a48cc1f237ead& 
grant_type=authorization_code& 
redirect_uri=https://myapp.com/auth/callback 

我当前的代码一直给我状态400我试图修改标题,但它不工作。我也试图使所需的参数部分路径使用?

const http = require('http'); 
    var options = { 
     hostname: 'app.sageone.com', 
     path: '/oauth2/token', 
     method: 'POST', 
     headers: { 
     "client_id":"xxxxx", 
     "client_secret":"xxxxx", 
     "code":"xxxxxx", 
     "grant_type":"authorization_code", 
     "redirect_uri":"https://some link" 
     } 
    }; 
    console.log('in users file point 2'); 
    var req1 = http.request(options, (res1) => { 
     console.log('statusCode:', res1.statusCode); 
     console.log('headers:', res1.headers); 
     console.log('message',res1.statusMessage); 
     res1.on('data', (d) => { 
     res.json(d); 
     }); 
    }); 

    req1.on('error', (e) => { 
     console.error('error starts here',e); 
    }); 
    req1.end(); 
}); 
+0

想必您的auth API是否可以通过https调用它?如果是这样,你需要['https'](https://nodejs.org/api/https.html)包而不是'http'。 – dan

回答

0

在我看来,你的问题不是Node.js,而是你使用Sage One的api。这是可以解决你的问题的the relevant documentation

从快速浏览看起来好像你想发送一个GET而不是POST,你应该在URL中发送这些参数。下面是他们给出的例子网址:

我以前从未使用鼠尾草一个

https://www.sageone.com/oauth2/auth?response_type=code&client_id=4b64axxxxxxxxxx00710&redirect_uri=https://myapp.com/auth/callback &范围= full_access,但是这将符合我与其他OAuth的API的经验。

+0

我已经完成了第一步并获得了代码。我试图获取我卡住的文档中提到的身份验证令牌。 – asis8888