OIDC Authorization - Bad Request - PHP

Following the information found here:

I am having some problems. I get the AUTH CODE without a problem. But then I go to post to the URL given:

https://api.twitch.tv/api/oauth2/token

This threw me a 404 so I figured the URL they meant was:

https://api.twitch.tv/kraken/oauth2/token

But following the flow is still giving me a 400 Bad Request error. Can someone help me know what I am doing wrong?

$url = 'https://api.twitch.tv/kraken/oauth2/token';	
$postdata = http_build_query(
	array(
		'client_id' => $cid,
		'client_secret' => $csec,
		'grant_type' => 'authorization_code',
		'redirect_uri' => $ruri,		
		'code' => $authCode								
	)
);

$opts = array('https' =>
	array(
		'method'  => 'POST',
		'header'  => 'Content-type: application/x-www-form-urlencoded',
		'content' => $postdata
	)
);

$context  = stream_context_create($opts);
$data = file_get_contents($url,false,$context);

Was a stupid typo…

$opts = array('http' =>
	array(
		'method'  => 'POST',
		'header'  => 'Content-type: application/x-www-form-urlencoded',
		'content' => $postdata
	)
);

Side note: you may want to change to using cURL here.

On most production boxes file_get_contents cannot perform http requests.

Heres a rough cURL example

<?php

if ($_GET['code']) {
    $token_url = 'https://api.twitch.tv/api/oauth2/token';
    $data = array(
        'client_id' => $client_id,
        'client_secret' => $client_secret,
        'grant_type' => 'authorization_code',
        'redirect_uri' => $redirect_uri,
        'code' => $_GET['code']
    );

    $curl = curl_init($token_url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    $result = curl_exec($curl);
    $i = curl_getinfo($curl);
    curl_close($curl);

    if ($i['http_code'] == 200) {
        $result = json_decode($result, true);

    }
}

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