$access_tokens doesn't contain an index oauth_token_secret

I was reading the tutorial here beause I don’t know anything about oauth. I got to this point…

$after_access_request = doHttpRequest($access_token_req->to_url());
parse_str($after_access_request,$access_tokens);

$access_token = new OAuthConsumer($access_tokens['oauth_token'], $access_tokens['oauth_token_secret']);

However when testing there isn’t an index $acccess_tokens[oauth_token_secret]. I did a dump of the values I was getting which were the following.

[url/oauth/request_token?oauth_consumer_key] => key
[oauth_nonce] => key
[oauth_signature] => key
[oauth_signature_method] => HMAC-SHA1
[oauth_timestamp] => 1374688924
[oauth_token] => key
[oauth_version] => 1.0errorInvalid OAuth request 

I replaced all of the keys with “key” since I’m not sure what they are used for and feel it would be really bad to simply post them here. As you can see though there is no oauth_consumer_secret also oauth_version has a strange string in it that says it was an invalid oauth request. I’ll include the whole code below.

function doHttpRequest($urlreq)
{
	$ch = curl_init();
	// set URL and other appropriate options
	curl_setopt($ch, CURLOPT_URL, "$urlreq");
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
	// grab URL and pass it to the browser
	$request_result = curl_exec($ch);
	// close cURL resource, and free up system resources
	curl_close($ch);
	return $request_result;
}
//helper function to get the stream_key from returned XML
function parseStream_KeyFromXML($xml)
{
    $xml_parser = xml_parser_create();

    xml_parse_into_struct($xml_parser, $xml, $vals, $index);
    xml_parser_free($xml_parser);

    if ($vals[1]['tag'] == "STREAM_KEY")
        return $vals[1]['value'];
    else
        return '';
}

$key = '';
$secret = '';

$base_url = 'http://localhost';
$token_url = 'http://api.twitch.tv/oauth/request_token';
$authorization_url = 'http://api.twitch.tv/oauth/authorize';

$consumer = new OAuthConsumer($key, $secret, NULL);

if ($_REQUEST['token'] == NULL) { //no token, request user authentication

	$sig_method = new OAuthSignatureMethod_HMAC_SHA1();
	$parsed = parse_url($token_url);
	$params = array(callback => $base_url);
	parse_str($parsed['query'], $params);

	$req_req = OAuthRequest::from_consumer_and_token($consumer, NULL, "GET", $token_url, $params);
	$req_req->sign_request($sig_method, $consumer, NULL);

	$req_token = doHttpRequest ($req_req->to_url());

	parse_str ($req_token,$tokens);

	$oauth_token = $tokens['oauth_token'];
	$oauth_token_secret = $tokens['oauth_token_secret'];

	$callback_url = "$base_url/?key=$key&token=$oauth_token&token_secret=$oauth_token_secret&endpoint="
    	                . urlencode($authorize_endpoint);

	$auth_url = $authorization_url . "?oauth_token=$oauth_token&oauth_callback=".urlencode($callback_url);

	Header("Location: $auth_url");
}
else{
	$token = $_REQUEST['token'];
	$token_secret = $_REQUEST['token_secret'];

	$auth_token = new OAuthConsumer($token, $token_secret);
	$access_token_req = new OAuthRequest("GET", $token_url);
	$access_token_req = $access_token_req->from_consumer_and_token($consumer,
    	            $auth_token, "GET", $token_url);

	$access_token_req->sign_request(new OAuthSignatureMethod_HMAC_SHA1(),$consumer,
	                $auth_token);

	$after_access_request = doHttpRequest($access_token_req->to_url());
	parse_str($after_access_request,$access_tokens);

	$access_token = new OAuthConsumer($access_tokens['oauth_token'], $access_tokens['oauth_token_secret']);

	print_r($access_tokens);

	$streamkey_req = $access_token_req->from_consumer_and_token($consumer,
    	            $access_token, "GET", "http://api.justin.tv/api/channel/stream_key.xml");

	$streamkey_req->sign_request(new OAuthSignatureMethod_HMAC_SHA1(),$consumer,$access_token);

	$after_request = doHttpRequest($streamkey_req->to_url());

	//Get streamkey from returned XML
	$stream_key = parseStream_KeyFromXML ($after_request);

	if ($stream_key == '')
    	echo ("Error getting stream_key from API!");
	else
	{   //We got the key! Embed the broadcaster and we're done.
    ?>
    <html>
    <OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
     codebase=""http://macromedia.com/cabs/swflash.cab#version=6,0,0,0""
     WIDTH="250" HEIGHT="250" id="flaMovie1" ALIGN="CENTER">
     <PARAM NAME=movie VALUE="http://www.justin.tv/widgets/live_embed_publisher.swf">
     <PARAM NAME=FlashVars VALUE="stream_key=<? echo $stream_key; ?>">
     <PARAM NAME=quality VALUE=high>
     <PARAM NAME=bgcolor VALUE=#FFFFFF>
     <embed src="http://www.justin.tv/widgets/live_embed_publisher.swf" FlashVars="stream_key=<? echo $stream_key; ?>"
      quality="high" bgcolor="#FFFFFF" WIDTH="250" HEIGHT="250"
      NAME="flaMovie1" ALIGN TYPE="application/x-shockwave-flash"
      PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">
    </OBJECT>
    </html>
    <?php
	}
}

You’re mixing up the APIs. JTV uses OAuth version 1, while Twitch uses version 2. So you shouldn’t be using the JTV guide to integrate with Twitch.

OAuth version 2 basically does away with all the signatures and different tokens, so you’ll just get one token straight up, which you’ll provide over HTTPS.

Read the Twitch authentication guide here: https://github.com/justintv/Twitch-API/blob/master/authentication.md

Thanks moocat, that helps a lot.