Getting twitch username PHP and oAuth

How can I get the twitch username (JUST the username) using this plugin? I can’t seem to find any simple guides out there on this subject. There isn’t a detailed guide on how to do this with PHP,

I am using this plugin:

I have everything setup for the plugin, now I need something where I can connect the user with that API, and then echo out the twitch username thats connected to my app.

OR if there is another way without using the plugin, let me know. I have tried several methods, and I can’t seem to develop a reliable script to simply get the username from the twitch app.


Truely, not being rude. We’re not here to write complete code for you or teach you a language.

Now that, that is over with. Just skimming over that php first you call authenticate and pass the returned url to the user to go to. Twitch will then return a “code” to your server/site and then you parse that out and then call get_access_token(code) api will then give you the access token. NOTE read the TOS in the readme on the github for twitch api. These “tokens” must be handled correctly.

After you get the token you then call authenticated_user(token) that will return the name object in the returned json file. You now have your username of the authenticated user. CONGRATS!

Following the readme for that php class.
It would be like this.
You have a page that uses the class and you do

< a href="<?php echo $twitchtv->authenticate() ?>">Authenticate Me</ a>

on your return uri page use the class again and do this.

<?php $ttv_code = $_GET['code']; $access_token = $twitchtv->get_access_token($ttv_code); $user_name = $twitchtv->authenticated_user($access_token); echo 'thank you ',$user_name,'for authenticating me!'; ?>

Thank you so much! I will definately start from there. I needed somewhere to start since I had trouble understanding the document. Now it makes sense!

I know this is an old topic but it’s the only one with an actual code example that I was looking for. I didn’t realize it’s the same one in xxplosions example until after I started playing with it. There is one very important piece of information missing in the above example though. Simply add this to the very end of xxplosions script and you’ll get a really quick example of a full authorization (from you to twitch) and uri redirect (from twitch back to you).

$twitchtv = new TwitchTV();
echo '<a href="'.$twitchtv->authenticate().'">Authenticate Me</a>';
echo "<br/>";

$twitchtv = new TwitchTV();
$ttv_code = $_GET['code'];
$access_token = $twitchtv->get_access_token($ttv_code);
$user_name = $twitchtv->authenticated_user($access_token);
echo 'Thank you '.$user_name.'!  Authentication Completed!';

You can rename the twitchtv.php file to whatever you’d like and include or require it from wherever you want.
For example: rename it to auth.php then include it from your index.php
If you have index.php set as your twitch client redirect uri it will still work.
The page you trigger the link from doesn’t matter. As long as the redirect uri in the script and on the twitch site are listed as the same then the script will work.

There is still a TON more work ahead of you. The class provided by xxplosions is only filled with helper functions. Unfortunately there isn’t a list of real world examples how to call each and every function in his class. I think that would have been nice.

Here’s a super easy barebones way to do a test authorization of your developer client (program/bot) using a link.

echo '<a href="">Authorize Me</a><br/>';


I’ve tested each example I’ve listed here and can assure their accuracy (until twitch decides to change everything again and again). Xxplosions class attempts to be easier to learn than IBurn36360’s but still has less than stellar examples. It does work and is certainly a heck of a lot easier than dealing with IBurn36360’s class for those new to PHP. I just started working with this stuff a few days ago. If anyone has comments/suggestions on helping to build a twitch web interface and chat moderation bot I’m all ears.

Thank you very much … this was really helpful

I know this is very late, but I have updated my class with some examples per @Devil’s recommendation. I will expand on it more to have an example of each function.