Basically I’ve followed the steps from here to get the app access token:
It successfully returns a token. Then I’m trying to get the userId from users I have the usernames, following the docs here:
The problem is, I’m getting this error:
401 - {"error":"Unauthorized","status":401,"message":"Invalid OAuth token"}
I don’t really understand what I’m doing wrong there, it should be possible to do that right? At least there are plenty of responses on google saying it is possible, and here too.
Here is the code that is not working:
const
request = require( 'request-promise-native' ),
{ TWITCH_CLIENT_ID, TWITCH_SECRET_KEY } = process.env;
function getAccessToken() {
return request({
method: 'POST',
json: true,
url: 'https://id.twitch.tv/oauth2/token',
qs: {
client_id: TWITCH_CLIENT_ID,
client_secret: TWITCH_SECRET_KEY,
grant_type: 'client_credentials'
},
})
.then( response => {
console.log( response );
return response.accessToken;
});
}
function getUserInfo( username ) {
return getAccessToken()
.then( bearerToken => request({
method: 'GET',
json: true,
url: 'https://api.twitch.tv/helix/users',
qs: { login: username },
headers: {
'Client-Id': TWITCH_CLIENT_ID,
Authorization: `Bearer ${ bearerToken }`
}
}))
.then( value => {
console.log( value );
});
}
getUserInfo( 'duhws' );
Any help would be the most welcome, thanks in advance!
Dist
2
That’s your issue. If you look at Getting OAuth Access Tokens | Twitch Developers or your console.log( response ) you’ll see that the response from Twitch has no key called accessToken, so response.accessToken is undefined and you’re trying to use Bearer undefined as your Authorization header.
It should be return response.access_token
1 Like
Also worth noting
Request and thus request-promise-native is deprecated and won’t be getting patches or updates!
1 Like
Oh my! Such a silly issue! Many thanks @Dist, on the response it is access_token instead of accessToken. Changed and it worked like a charm!
@BarryCarlyon Good point, I’m aware it is now on maintenance mode, still considering which lib to replace it, do you recommend any?
Request complied a list on ticket 3143
Personally I moved to got
1 Like
system
Closed
6
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.