2016-09-23 4 views
0

Я делаю приложение, которое должно использовать oAuth для аутентификации игроков с серверов Blizzard, я хочу получить доступ к их информации о символах .. и я не могу понять, как попросить secret_token. Я предполагаю, что я делаю мой запрос пост неправильно ниже код, я используюoAuth code exchange for 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); 
    } 
}); 

поэтому в основном я получаю код делает запрос на запись к моему серверу с ним, а затем вызвав пост запрос на сервер blizzard пытается обменять мой код на токен доступа.

Ошибки я получаю:

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

Я использую Node.js & request.js сделать пост, моя догадка я не делаю надлежащий запрос запроса поста?

ответ

0

Я думаю, body ключ не принимается в request.

Отправить data в json если content-type является JSON или form если content-type является x-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) 
     } 

    });