Problem using /helix/users endpoint to get user info via Bearer token

Hi all,

Got some code which successfully goes through the user authorization process and gets called back with a bearer token.

I then try and use the token to get the user’s info via https://dev.twitch.tv/docs/api/reference/#get-users namely “If neither a user ID nor a login name is specified, the user is looked up by Bearer token.”

In node.js I try…

var options = {
    hostname: 'api.twitch.tv',
    port: 443,
    path: '/helix/users',
    method: 'GET',
    headers: {
        'Authorization': 'Bearer ' + sessionData.code,
        'Client-ID': client_id
    }
};
https.get(options, function(res) {
    etc...

However, I get back…

{
    "error": "Bad Request",
    "status": 400,
    "message": "Must provide an ID, Login or OAuth Token."
}

Any idea would be greatly appreciated :slight_smile:
Thx

Try passing the Bearer token and the Client-ID as query parameters instead of as headers.

I have my code set to use either the Bearer OR the Client-ID but not both.

Also hopefully in your GET function you are specifying either logins or ids to get. I’m not sure what would happen if you did not specify any.

At a second glance, I’m not sure why it’s not working because I also pass the Bearer token and/or Client-ID as headers.

If you don’t supply any user id’s or logins, the endpoint will get the information based on the Bearer token provided.

Passing a params was a great idea, just tried it, but still no luck (same error) :frowning:

For reference, I tried

            var options = {
                hostname: 'api.twitch.tv',
                port: 443,
                path: '/helix/users?Client-ID=' + client_id + "+Bearer=" + sessionData.code,
                method: 'GET',
                headers: {
                    'Authorization': 'Bearer ' + sessionData.code,
                    'Client-ID': client_id
                }
            };

Good idea though

Six: you send both the Bearer and the client-id in the same request’s header and it works? If so that’s good info.

I believe OP may need to make sure he is using a POST not a GET.

b: Tried changing to POST (don’t trust the documentation mode on) but the API returned

{
    "error": "Not Found",
    "status": 404,
    "message": ""
}

Yeah, you can send both in a request and it should work fine. Although I’ve yet to do enough testing figure out exactly what happens on Twitch’s back end. From what I have tested so far, it seems like it uses the Bearer token for authorization (obviously) and determining rate limit, and it uses the the Client ID to identify the request if both are provided. Although there’s a decent chance I could be wrong.

In this case, you want to be using GET if you are retrieving a user’s data. You would want to use POST if you are updating a user’s description.

The error is most likely in your authorization process. You’re calling it sessionData.code, hopefully not after the code parameter from Twitch, which you’re supposed to exchange for the access_token.

1 Like

A ha, that sounds like a possible issue. I shall add the extra step…

In my code I only use the client_id to get the Bearer token then I don’t use the Client-ID anymore.

Ah yes, I looked at the wrong line of code, GET is correct here.

 36     url = 'https://api.twitch.tv/helix/users'
 37     parameters = {'login': '(username)'}
 38     if len(access_token):
 39         print "using access_token"
 40         headers = {'Authorization': 'Bearer ' + access_token}
 41     else:
 42         headers = {'Client-ID': client_id}
 43     r = requests.get(url, headers=headers, params=parameters)

3ventic: Genius, that was the problem (exchange code for access_token)

All working great now!

Thanks :slight_smile:

1 Like

Thanks for the code but I wanted the authenticated user’s info (i.e. I don’t know the username). Good to know for future though. Thx

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.