Created
April 9, 2010 11:31
-
-
Save ydn/361071 to your computer and use it in GitHub Desktop.
example code for doing the OAuth dance w/ Netflix in php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // simple example code for doing the OAuth dance w/ netflix in php | |
//http://oauth.net/code/ | |
//http://oauth.googlecode.com/svn/code/php/OAuth.php | |
require 'oauth.php'; | |
//key/secret from http://developer.netflix.com | |
$key = 'slkdjf298374'; | |
$secret = 'lskdjf0298374'; | |
$consumer = new OAuthConsumer($key, $secret); | |
$sig_method = new OAuthSignatureMethod_HMAC_SHA1; | |
$storage = json_decode( file_get_contents( 'storage.json' ) ); | |
if($storage->access_token){ // proxy request | |
var_dump($storage); | |
}elseif($_GET['oauth_token']){ // auth callback | |
$token = new OAuthToken( $storage->request_token->oauth_token, $storage->request_token->oauth_token_secret ); | |
unset( $storage->request_token ); | |
//call this file | |
$api_endpoint = 'http://api.netflix.com/oauth/access_token'; | |
//handle request in 'server' block above | |
$parameters = array( | |
'oauth_token' => $_GET['oauth_token'] | |
); | |
//use oauth lib to sign request | |
$req = OAuthRequest::from_consumer_and_token($consumer, $token, "GET", $api_endpoint, $parameters); | |
$sig_method = new OAuthSignatureMethod_HMAC_SHA1(); | |
$req->sign_request($sig_method, $consumer, $token);//note: double entry of token | |
//get data using signed url | |
$ch = curl_init($req->to_url()); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$data = curl_exec($ch); | |
curl_close($ch); | |
parse_str($data, $parsed); | |
$storage->access_token = $parsed; | |
file_put_contents('storage.json', json_encode( $storage ) ); | |
}else{ // fetch request token | |
//call this file | |
$api_endpoint = 'http://api.netflix.com/oauth/request_token'; | |
//handle request in 'server' block above | |
$parameters = array(); | |
//use oauth lib to sign request | |
$req = OAuthRequest::from_consumer_and_token($consumer, null, "GET", $api_endpoint, $parameters); | |
$sig_method = new OAuthSignatureMethod_HMAC_SHA1(); | |
$req->sign_request($sig_method, $consumer, null);//note: double entry of token | |
// get data using signed url | |
$ch = curl_init($req->to_url()); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$data = curl_exec($ch); | |
curl_close($ch); | |
// extract params & build auth uri | |
parse_str($data, $parsed); | |
// store token | |
file_put_contents('storage.json', json_encode( array( 'request_token' => $parsed ) ) ); | |
$params = array( | |
'oauth_token' => $parsed['oauth_token'], | |
'oauth_callback' => 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'], | |
'oauth_consumer_key' => $key, | |
'application_name' => $parsed['application_name'], | |
); | |
$auth_uri = $parsed['login_url'].'&'.http_build_query( $params ); | |
printf('<a href="%s">click</a>', $auth_uri); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment