PHP Authentication Basic Setup

Hello,

I am not very familiar with the Twitch API and I’m no PHP expert, I’d appreciate any help.

I am currently trying to set up authentication/authorization with the Twitch API in PHP using the following documentation: https://github.com/justintv/Twitch-API/blob/master/authentication.md#authorization-code-flow

I was able to connect and generate authentication code with using the following PHP/Javascript:

<?php
$auth_token = "'https://api.twitch.tv/kraken/oauth2/authorize?response_type=code 
    &client_id=[client_id]
    &redirect_uri=[redirect_uri]
    &scope=[scope];
?>
<script>
    window.location = <?php echo($auth_token) ?>;
</script>
//Twitch then redirects back to my application and I have the code
<?php
    if(isset($_GET["code"])){
      $user_code = $_GET["code"];
    }
?>

After I have the code I need for step 2 in the documentation I use the following PHP to try to POST to complete step 3:

  $url = 'https://api.twitch.tv/kraken/oauth2/token';
  $fields = array(
    'client_id' => $auth_client_id,
    'client_secret' => $auth_client_secret,
    'grant_type' => 'authorization_code',
    'redirect_uri' => $auth_redirect_uri,
    'code' => $user_code,
    'state' => null
  );
  // use key 'http' even if you send the request to https://...
  $options = array(
      'http' => array(
          'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
          'method'  => 'POST',
          'content' => http_build_query($fields),
      ),
  );
  $context  = stream_context_create($options);
  $result = file_get_contents($url, false, $context);

  var_dump($result);

At this point I am lost, the PHP response generates the following error:

Warning: file_get_contents(https://api.twitch.tv/kraken/oauth2/token): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request

All I want to do is allow users to connect their twich accounts with my web app and use it for login/authentication purposes. Can anyone point me in the right direction?

Thank you in advance.

Hi Dreink!

I’m also a bit new to the Twitch API, but I think you need to change your header.

According to Twitch API MIME types, (https://github.com/justintv/Twitch-API) you should use the following as header. (Not sure if this solve your problem tho).

application/vnd.twitchtv.v3+json 

What I would suggest is to use cURL instead to make a HTTP request.

A simple cURL request would look like the following:

$curl = curl_init();
	$arr = array(
		'client_id' => 'Client_id',
		'client_secret' => 'Client_secret',
		'grant_type' => 'authorization_code', //Do not change this
		'redirect_uri' => 'Redirect_uri',
		'code' => $user_code,
                'state' => NULL
		);
	curl_setopt_array($curl, array(
		CURLOPT_SSL_VERIFYPEER => FALSE,
		CURLOPT_FOLLOWLOCATION => FALSE,
		CURLOPT_URL => 'https://api.twitch.tv/kraken/oauth2/token/',
		CURLOPT_POSTFIELDS => $arr,
		CURLOPT_RETURNTRANSFER => TRUE
		));
        $resp = curl_exec($curl);
        curl_close($curl);

(Not sure if above really works, but it should - have not tried it out yet in PHP)

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