Get user data from a token

Hi, i’m studying web developing and for exercise i’m trying to create a site where the user access with twitch, but i don’t know how to take the user’s data (mainly I would like the username), i’m following the “OAuth Authorization Code Flow” and i’ve arrived at the point that i’ve an access_token, what i’ve to do at this point? (if is the right way to arrive at what i want)

You can use the Get Users endpoint https://dev.twitch.tv/docs/api/reference/#get-users to get user information based on an access token by sending it as the Authorizaiton: Bearer YOUR_ACCESS_TOKEN header.

1 Like

Thanks for your help Dist!
I’ve tried but i get a 400 error.
i get the code with “https://id.twitch.tv/oauth2/authorize?response_type=code&client_id=&redirect_uri=http://localhost:3000/&scope=user_read”
then i pass it to POST “https://id.twitch.tv/oauth2/token?client_id=&client_secret=&code=” + code + “&grant_type=client_credentials&redirect_uri=http://localhost:3000/” and with the token that i receve i do
fetch(“https://api.twitch.tv/helix/users”, {
headers: {
Authorization: "Bearer " + access_token
}
})

If you look at the body of the response it should contain a more detailed reason why the request failed

GET https://api.twitch.tv/helix/users 400 () Home.js:19

  1. Response {type: “cors”, url: “https://api.twitch.tv/helix/users”, redirected: false, status: 400, ok: false, …}

  2. body:ReadableStream

    1. locked:true
    2. proto:Object
  3. bodyUsed:true

  4. headers:Headers

    1. proto:Headers
  5. ok:false

  6. redirected:false

  7. status:400

  8. statusText:""

  9. type:“cors”

  10. url:“https://api.twitch.tv/helix/users

  11. proto:Response

that’s all i get, but i don’t know what mean

What do you get if you do something like:

fetch("https://api.twitch.tv/helix/users",
    {
        headers: {
            Authorization: 'Bearer ' + BearerToken
        }
    }
).then(function(c) {
    return c.json()
}).then(function(j) {
    console.log(j)
}).catch(function(err) {
    console.log('e', err);
});

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

Seems like your bearertoken is no go!

I’ve tried to use the “OAuth Implicit Code Flow” instead of the " OAuth Authorization Code Flow" and it work!
Thank you all for your help!

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