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.
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
-
Response {type: “cors”, url: “https://api.twitch.tv/helix/users”, redirected: false, status: 400, ok: false, …}
-
body:ReadableStream
- locked:true
- proto:Object
-
bodyUsed:true
-
headers:Headers
- proto:Headers
-
ok:false
-
redirected:false
-
status:400
-
statusText:""
-
type:“cors”
-
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.