Hey
Firstly I am an absolute novice at web development. All I really want is to authenticate through twitch and initially get their username.
I have read https://github.com/justintv/Twitch-API/blob/master/authentication.md#auth-code but have not fully understood it.
I am using this https://github.com/Xxplosions/twitchtv-oauth as it seemed to be the friendliest code I could find.
So I can send the user to authenticate my application, once authorised I can receive my code, I am then using Xxplosions’ class to sort out getting the access token:
class TwitchTV {
var $base_url = "https://api.twitch.tv/kraken/";
var $client_id = 'removed - INSET ID HERE'; //change this value, should be your TwitchTV Application Client ID
var $client_secret = "removed - INSET ID HERE"; //change this value, should be your TwitchTV Application Client Secret
var $redirect_url = 'http://localhost/test/try.php'; //change this value, should be your TwitchTV Application Rerdirect URL
var $scope_array = array('user_read','channel_read','chat_login','user_follows_edit','channel_editor','channel_commercial','channel_check_subscription');
/**
* Channel data for the fetched user
*
* @var stdClass
*/
var $channel_data = null;
var $curl_cache;
public function __construct(){
$this->curl_cache = new TwitchTV_Curl_Cache();
}
function get_access_token($code) {
$ch = curl_init($this->base_url . "oauth2/token");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$fields = array(
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->redirect_url,
'code' => $code
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$data = curl_exec($ch);
$response = json_decode($data, true);
return $response["access_token"];
}
I think my main problem here is, I have no idea how to use that return value, I have tried to echo (just to confirm it is working) it out like this:
$ttvcode = $_GET['code'];
$twitchtv = new TwitchTV;
$authcode = $twitchtv->get_access_token($ttvcode));
echo $authcode;
but when my link is directed to this page I just get a blank page. I am presuming this is because I am messing up dealing with the JSON.
I’m not asking you to do all my work for me, but if someone can provide relevant tutorials or more in depth guides to this kind of stuff, I would love you for ever.
I’m trying to do this mostly in PHP and if something I have said makes no sense, I apologise, and will try and give you more info if requested.
Sorry and thanks!