human art and other bits


Signing into the TradeMe API with PHP and ZendFramework Oauth

, posted: 27-Jan-2011 22:47

I have recently been playing with the new TradeMe API,  it uses the OAuth system to login. Which is pretty cool as it lets anyone build applications that use the TradeMe API without the need of the users to give up their Username or Passwords.
 
I have worked out how to do this with the help of Zend Frameworks Oauth Documentation,
the WebHole blog post titled Zend OAuth with Twitter and of course the TradeMe API documentation.
 
The code example below has a lot of extra print_r and echo statements, these  help  display what is going on.
There are three files main.php, oauthconfig.php and callback.php
Feel free to ask questions.  I hope to write more about the Trademe API as I start to understand it.
 
main.php
1: 5: TradeMe OAuth Login 6:   9: <?php 10: session_start(); 11: // include the oauth consumer class 12: require_once 'Zend/Oauth/Consumer.php'; 13: require_once 'oauthconfig.php'; 14:   15: echo 'config object'; 16: print_r($config); 17: 18: // create oauth object 19: $oauth=new Zend_Oauth_Consumer($config); 20:   21: echo 'oauth object'; 22: print_r($oauth); 23: 24:   25: echo 'request token'; 26:   27: // get request token 28: try{ 29: $request_token = $oauth->getRequestToken(); 30: } 31: catch(Exception $e) 32: { 33: echo 'Error: '.$e->getMessage(); 34: print_r($e); // show debug infomation 35: exit (1); 36: } 37: // store request token in session 38: $_SESSION['request_token']=serialize($request_token); 39:   40: echo '<br>the request token: '.$request_token; 41:   42: // explode request token to extract oauth token 43: $exploded_request_token=explode('=',str_replace('&','=',$request_token)); 44: // get oauth token from exploded request token 45: $oauth_token=$exploded_request_token[1]; 46: echo '
the exploded request token a<br>'; 47: print_r($exploded_request_token); 48: echo '<br><br>'; 49:   50: echo 'request trademe for Authorized token.<br>'; 51: // show sign in link 52: echo "< a href='https://secure.trademe.co.nz/Oauth/Authorize?oauth_token={$oauth_token}'>
request authorize"; 53:   54: ?>  
oauthconfig.php
0: $consumerKey='consumer key provided by trademe'; 0a: $consumerSecret='consumer secret key provided by trademe'; 0b: $config=array( 1: 'requestScheme' =>Zend_Oauth::REQUEST_SCHEME_HEADER, 12: 'version'=>'1.0', 13: 'signatureMethod'=>'HMAC-SHA1', 14: 'callbackUrl' => 'http://localhost/TradeMeAPI/callback.php', // where to go after authorized 15: 'siteUrl' => 'https://secure.trademe.co.nz/Oauth/', 16: 'requestTokenUrl' => 'https://secure.trademe.co.nz/Oauth/RequestToken', 17: 'userAuthorisationUrl' => 'https://secure.trademe.co.nz/Oauth/Authorize', 18: 'accessTokenUrl' => 'https://secure.trademe.co.nz/Oauth/AccessToken', 19: 'consumerKey'=>$consumerKey, 20: 'consumerSecret'=>$consumerSecret, 21: 'oauth_timestamp'=>time(), 22: 'oauth_nonce'=>rand(1, 1000000), 23: 24: 'oauth_signature'=>'' 25: ); 26: ?>  
callback.php
1: we have callback'; 7:   8: session_start(); 9: require_once 'Zend/Oauth/Consumer.php'; 10: require_once 'oauthConfig.php'; 11:   12: $oauth = new Zend_Oauth_Consumer($config); 13:   14: if (isset($_GET['oauth_token']) && isset($_SESSION['request_token'])) { 15: try{ 16: $access = $oauth->getAccessToken($_GET, unserialize($_SESSION['request_token'])); 17: } 18: catch(Exception $e) 19: { 20: echo 'Error: '.$e->getMessage(); 21: exit (1); 22: } 23:   24: echo 'access tokens'; 25: echo $access; 26: $_SESSION['access_token'] = serialize($access); 27: $_SESSION['request_token'] = null; 28:   29: /* 30: * 31: * This is where the trademe interactions happen 32: * 33: */ 34:   35: }elseif(!empty($_GET['denied'])){ 36: echo 'you have denied us access to your trademe crendentials'; 37: } 38: else { 39: echo 'Invalid callback request. Oops. Sorry.'; 40: } 41: ?>


Tag(s):         


Other related posts:
EASEUS Todo Backup – my favourite backup software
Jolicloud on the netbook
A couple of new things on my EeePC 1001HA






Author's note by hellonearthisman, on 28-Jan-2011 00:42

Argh Geekzone, ok, so I'm having problem with posting code examples at so much it stripped to prevent cross site scripting. Working on a solution.


Comment by freitasm, on 28-Jan-2011 10:10

The code is not that long - anyone wanting to use snippets can easily copy and work from there...

As for posting code snippets, let's see what can be done. 


Comment by rotamota, on 8-Jun-2011 12:25

Once you have connected whats next? How can you retrieve account details using the access token?


Add a comment

Please note: comments that are inappropriate or promotional in nature will be deleted. E-mail addresses are not displayed, but you must enter a valid e-mail address to confirm your comments.

Are you a registered Geekzone user? Login to have the fields below automatically filled in for you and to enable links in comments. If you have (or qualify to have) a Geekzone Blog then your comment will be automatically confirmed and placed in the moderation queue for the blog owner's approval.

Your name:

Your e-mail:

Your webpage:

hellonearthisman's profile

Brett Cooper
Feilding
New Zealand


Stone artist with a digital understanding. 1st computer was a c=64