2016-09-23 50 views
0

我正在製作一個應用程序,該應用程序應該使用oAuth從暴雪服務器認證玩家,我想訪問他們的角色信息..我無法弄清楚如何請求secret_token。我想我做錯了我下面的POST請求我使用oAuth代碼交換祕密標記

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); 
    } 
}); 

所以基本上我得到的代碼做一個POST請求到我的服務器,然後觸發一個POST請求的代碼暴雪服務器試圖將我的代碼換成訪問令牌。

我得到的錯誤是:

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

我使用Node.js & request.js,使後,我的猜測是,我不是做一個適當的請求後要求?

回答

0

我認爲body密鑰在request不可接受。

送在jsondata如果content-typeJSONform如果content-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); 
    } 
}); 
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) 
     } 

    });