Get id from the connected user

Hello,

when a user is connected with Twitch account, I want to get his id.

But I don’t know how to make this.

Do you have an idea ?

Regards.

When a user has connected with their Twitch account where?

When a user is connected with this link and redirected to my website.

https://id.twitch.tv/oauth2/authorize?client_id=*****&response_type=token&scope=user:edit+user:read:email&redirect_uri=http://localhost/

That gives you a code

You take the code and exchange the code for a token.

Then you can use the token with any of the twitch API’s.

Usually this endpoint:

Which if you don’t provide a ID will give you the data for the authenticating user, and optionally include email if the token you use has the right scope

Thanks for the reply, when I try to request https://api.twitch.tv/helix/users when I’m logged. I have an error 400 :
{“error”:“Bad Request”,“status”:400,“message”:“Must provide an ID, Login or OAuth Token.”}

You are trying this in a web browser? It won’t work with a web browser unless you are sending the relevant headers. As documented.

The error message tells you what to do.

It would seem after doing the oAuth dance you didn’t apply the oAuth token to your request.

I try this in JavaScript :

var myHeaders = new Headers();
myHeaders.append(“Client-ID”, twitchID);
const response = await fetch(twitchUserLink, {method: ‘GET’, mode: ‘cors’, headers: myHeaders});

Sorry I don’t know how to include code.

You provided only a ClientID, you didn’t provide a ID, Login or OAuth Token, as the error message says

Ok, when I try to provide the OAuth Token I have this error :

Request header field oauth-token is not allowed by Access-Control-Allow-Headers in preflight response.

As per the docs I linked to in my first reply.

Which provides an example request:

curl -H 'Authorization: Bearer cfabdegwdoklmawdzdo98xt2fo512y' \
-X GET 'https://api.twitch.tv/helix/users?id=44322889'

The header is Authorization and you should preceed your token with Bearer since you provide a Bearer to can omit ?id=Whatever

Yes ! It’s working perfectly !

Thanks a lot !

You’r the best :wink:

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