0

我正在制作一个应该使用 oAuth 对来自暴雪服务器的玩家进行身份验证的应用程序,我想访问他们的角色信息……但我不知道如何请求 secret_token。我想我的发布请求错误如下是我正在使用的代码

app.post('/', function(req, res) {

      var code = req.body.code; //this is the code i get ounce the player is redirected back to my redirect_uri
      var redirectUri = "https://localhost:3000/oauth_callback.html";
      var scope = "wow.profile";

      var key = "they client_id i was given";
      var secret = "they secret I was given";

      var grantType = "authorization_code";
      var tokenUri = "https://us.battle.net/oauth/token";
      var uriBody = "?client_id=" + key + "&client_secret=" + secret + "&grant_type=authorization_code&code=" + code + "&redirect_uri=" + redirectUri + "&scope=" + scope;


  request({
    url: tokenUri, //URL to hit
    method: 'POST',
    headers: { 
        'Content-Type': "application/x-www-form-urlencoded",
    },
    body: uriBody //Set the body as a string
}, function(error, response, body){
    if(error) {
        console.log(error);
    } else {
        console.log(response.statusCode, body);
    }
});

所以基本上我正在获取代码向我的服务器发出发布请求,然后触发对暴雪服务器的发布请求,试图将我的代码交换为访问令牌。

我得到的错误是:

401 '{"error":"unauthorized","error_description":"An Authentication object was not found in the SecurityContext"}'

我正在使用Node.js&request.js发布帖子,我猜我没有发出正确的请求发布请求?

4

2 回答 2

0

最后!这就是我如何让它工作的!qs = query-string.js 库...

var token_params = qs.stringify({
      client_id: key,
      client_secret: secret,
      code: code,
      scope: scope,
      grant_type: 'authorization_code',
      redirect_uri: redirectUri
    });

    request('https://us.battle.net/oauth/token?' + token_params, function(error, response, body){
      if (error) {
        console.log(error);
      } else {
        console.log(body) 
      }

    });
于 2016-09-23T20:11:50.213 回答
0

我认为bodykey 在request.

发送if is or if is data_jsoncontent-typeJSONformcontent-typex-www-form-urlencoded

像这样

request({
    url: tokenUri, //URL to hit
    method: 'POST',
    headers: { 
        'Content-Type': "application/x-www-form-urlencoded",
    },
    form: uriBody //Set the body as a string
}, function(error, response, body){
    if(error) {
        console.log(error);
    } else {
        console.log(response.statusCode, body);
    }
}); 
于 2016-09-23T17:46:01.377 回答