![](/img/trans.png)
[英]How to properly store clientId, client secret and OAuth2 token with ReactApp
[英]oAuth code exchange for secret token
我正在制作一個應使用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
進行發布,我的猜測是我沒有做出適當的請求發布請求?
我認為按request
不能接受body
密鑰。
如果
content-type
為JSON
,json
form
發送data
;如果content-type
為x-www-form-urlencoded
發送data
像這樣
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);
}
});
最后! 這就是我的工作方式! 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)
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.