Basically I’ve followed the steps from here to get the app access token:
Getting Tokens: OAuth
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
August 28, 2020, 9:11am
#2
That’s your issue. If you look at https://dev.twitch.tv/docs/authentication/getting-tokens-oauth#oauth-client-credentials-flow 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?
thiagomorato:
@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
opened 02:41AM - 01 Apr 19 UTC
neverstale
Since the announcement of request going into "maintenance mode" (full details in… #3142) I'd like to collect a list of alternative libraries to use. Please comment below and I'll update this table. When we have a list of good alternatives we should add this to the readme.
In no particular order and dreadfully incomplete;
Package Name | Bundle Size | API Style | Summary
------------ | ------------- | ------------- | -------------
[node-fetch](https://www.npmjs.com/package/node-fetch)| [0.4kb](https://bundlephobia.com/result?p=node-fetch@2.3.0) | promise / stream | A light-weight module that brings window.fetch to Node.js
[bent](https://github.com/mikeal/bent) | [1kb](https://bundlephobia.com/result?p=bent@6.1.0) | fp / promise / stream | Functional HTTP client w/ async/await
[got](https://www.npmjs.com/package/got) | [48.4kb](https://bundlephobia.com/result?p=got@9.6.0) | promise / stream | Simplified HTTP requests
[make-fetch-happen](https://www.npmjs.com/package/make-fetch-happen) | [442kb](https://bundlephobia.com/result?p=make-fetch-happen@4.0.1) | promise / stream | make-fetch-happen is a Node.js library that wraps node-fetch-npm with additional features node-fetch doesn't intend to include, including HTTP Cache support, request pooling, proxies, retries, and more!
[axios](https://www.npmjs.com/package/axios) | [11.9kb](https://bundlephobia.com/result?p=axios@0.18.0) | promise / stream | Promise based HTTP client for the browser and node.js
[unfetch](https://www.npmjs.com/package/unfetch) | [1kb](https://bundlephobia.com/result?p=unfetch@4.1.0) | promise / stream | Tiny 500b fetch "barely-polyfill"
[superagent](https://www.npmjs.com/package/superagent) | [18kb](https://bundlephobia.com/result?p=superagent@5.0.2) | chaining / promise | Small progressive client-side HTTP request library, and Node.js module with the same API, sporting many high-level HTTP client features
[tiny-json-http](https://www.npmjs.com/package/tiny-json-http) | [22kb](https://bundlephobia.com/result?p=tiny-json-http@7.0.2) | promise | Minimalist HTTP client for GET and POSTing JSON payloads
[needle](https://www.npmjs.com/package/needle) | [164kb](https://bundlephobia.com/result?p=needle@2.2.4) | chaining / promise | The leanest and most handsome HTTP client in the Nodelands
[urllib](https://www.npmjs.com/package/urllib) | [816kb](https://bundlephobia.com/result?p=urllib@2.33.2) | callback / promise | Help in opening URLs (mostly HTTP) in a complex world — basic and digest authentication, redirections, cookies and more.
Personally I moved to got
1 Like
system
closed
September 27, 2020, 12:54pm
#6
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.