此模块(https://www.npmjs.com/package/twitch-api-v5)告诉我我的OAuth令牌无效。我不确定为什么。我一定使用了错误的信息或其他信息,但对我来说似乎一切正常。希望有人对这些细节足够熟悉,以帮助我。
api.clientID = auth.client_id;
req_url = 'https://id.twitch.tv/oauth2/token?client_id='+auth.client_id+'&client_secret='+auth.client_secret+'&grant_type=client_credentials&scope=channel_subscriptions+user_subscriptions+channel_check_subscription';
var req_options = {
url:req_url,
method: 'POST'
}
request(req_options, function (error, response, body) {
var temp = JSON.parse(body);
console.log(temp);
access_token = temp.access_token;
});
这很好:
{ access_token: 'xxxxx',
expires_in: 5664903,
scope:
[ 'channel_subscriptions',
'user_subscriptions',
'channel_check_subscription' ],
token_type: 'bearer' }
然后我尝试使用该令牌运行此代码:
api.channels.subs({auth:access_token,channelID:auth.id},(err,res)=>{
if(err){
console.log("API Error: "+err);
}else{
console.log(res);
}
})
进展不顺利:
{ error: 'Unauthorized',
status: 401,
message: 'invalid oauth token' }
所以我检查令牌的有效性:
api.auth.checkToken({auth:access_token},(err,res)=>{
if(err){
console.log("API Error: "+err);
}else{
console.log(res);
}
})
一切都很好:
{ token:
{ valid: true,
authorization:
{ scopes: [Array],
created_at: '2018-09-06T12:17:43Z',
updated_at: '2018-09-06T12:17:43Z' },
client_id: 'xxxxx',
expires_in: 5664603 } }