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.