Advertisement
Guest User

Tumblr Oauth API

a guest
Oct 22nd, 2010
2,362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.31 KB | None | 0 0
  1. <?php
  2.  
  3. class userTokenTumblr {
  4.     var $consumer_key;
  5.     var $consumer_secret;
  6.     var $login_url;       // Tumblr url for require login permission
  7.     var $request_token;   // Tumblr Request Token
  8.     var $access_token;    // Tumblr Access token array(oauth_token, oauth_token_secret)
  9.     var $oauth_token;
  10.     var $oauth_token_secret;
  11.     var $username;
  12.    
  13.     var $service_url = 'http://www.tumblr.com';
  14.     var $request_token_url = 'http://www.tumblr.com/oauth/request_token';
  15.     var $authorize_url = 'http://www.tumblr.com/oauth/authorize';
  16.     var $access_token_url = 'http://www.tumblr.com/oauth/access_token';
  17.     var $authenticate_url = 'http://www.tumblr.com/api/authenticate';
  18.     var $read_url = 'http://www.tumblr.com/api/read';
  19.     var $write_url = 'http://www.tumblr.com/api/write';
  20.        
  21.     var $tumblrOauthObject;
  22.    
  23.     function __construct() {
  24.         $this->consumer_key = $GLOBALS['tumblrConsumerKey'];
  25.         $this->consumer_secret = $GLOBALS['tumblrConsumerSecret'];
  26.         // We create the Oauth object as a mehtod's property.
  27.         //Very important, the use of OAUTH_AUTH_TYPE_URI for general purpose, although we may need to use a diferent method for diferent tasks
  28.         $this->tumblrOauthObject = new OAuth($this->consumer_key,$this->consumer_secret,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);
  29.         //$this->tumblrOauthObject->enableDebug();
  30.     }
  31.  
  32.     //Retrieve the tokens from DB.    
  33.     function GetDBAccessTokens($uIdentifier, $uId) {
  34.         $usertokenservice =& ServiceFactory::getServiceInstance('UserTokenService');
  35.         $data = $usertokenservice->GetData($uIdentifier, $uId, 'tumblr');
  36.         $unserializedata=unserialize($data['data']);
  37.         $tokens['oauth_token']=$unserializedata['oauth_token'];
  38.         $tokens['oauth_token_secret']=$unserializedata['oauth_token_secret'];
  39.        
  40.         return $tokens;
  41.     }
  42.  
  43.     function RequestLogin() {
  44.         //With this lines, we retrieve the login url, and redirect the user there.
  45.         $response = $this->tumblrOauthObject->getRequestToken($this->request_token_url);
  46.         $_SESSION['secret'] = $response['oauth_token_secret'];
  47.         header("Location: {$this->authorize_url}?oauth_token={$response["oauth_token"]}");
  48.         exit();
  49.     }
  50.    
  51.     function CallBackAuth() {
  52.         if (isset($_GET['oauth_token'])
  53.            || (isset($_SESSION['oauth_token']) && isset($_SESSION['oauth_token_secret'])) ) {
  54.  
  55.             if( !isset($_SESSION['oauth_token']) || !isset($_SESSION['oauth_token_secret']) ) {
  56.                 //If we havent sessions tokens, its the first time here for the user, so we put in the oauth object,
  57.                 // The oauth_token from $_GET and the secret token retrieved in the RequestLogin method. (saved in session)
  58.                 // This is a temporary token, how allows us to retrieve the definitive tokens, the one that we will save to db.
  59.                 $this->tumblrOauthObject->setToken($_GET['oauth_token'], $_SESSION['secret']);
  60.                 //GetAccessToken retrieves the permanent tokens for the tumblr user.
  61.                 $token = $this->tumblrOauthObject->getAccessToken($this->access_token_url);
  62.                 //Once we have the definitive tokens, we save it temporarly in SESSION, to avoid loosing data between redirections.
  63.                 $_SESSION['oauth_token'] = $token['oauth_token'];
  64.                 $_SESSION['oauth_token_secret'] = $token['oauth_token_secret'];            
  65.                  
  66.                 // pass deffinitive tokens to Tumblr object
  67.                 $this->tumblrOauthObject->setToken($token['oauth_token'], $token['oauth_token_secret']);
  68.                 $token['oauth_token'] = $_SESSION['oauth_token'];
  69.                 $token['oauth_token_secret'] = $_SESSION['oauth_token_secret'];
  70.             } else {
  71.                 //If we already have SESSION stored tokens, we set this tokens for the Oauth Object.
  72.                 $token['oauth_token']=$_SESSION['oauth_token'];
  73.                 $token['oauth_token_secret']=$_SESSION['oauth_token_secret'];
  74.                 $this->tumblrOauthObject->setToken($token->oauth_token, $token->oauth_token_secret);
  75.             }
  76.         } else {
  77.             //If no GET oauth token is set, or no SESSION token, we proceed retrieveng permissions to the user
  78.             $this->RequestLogin('tumblr');
  79.         }
  80.        
  81.         $params = array(
  82.                 'oauth_token' => $_SESSION['oauth_token']
  83.             );
  84.         $tumblrInfo = $this->tumblrOauthObject->fetch($this->authenticate_url, $params);
  85.        
  86.         $tumblrInfo = $this->tumblrOauthObject->getLastResponse();
  87.         $xml = new SimpleXMLElement($tumblrInfo);
  88.        
  89.         //Now in $xml we have the user information.
  90.        
  91.         //The next steps are optional, and we use it to normalize the diferent social apis output.
  92.         //This class is called from a wrapeer how encapsulates the diferent API calls, so we provide the wrapper with a normalized
  93.         //output always.
  94.        
  95.         $response['provider'] = 'tumblr';
  96.  
  97.         $response['username'] = (string) $xml->tumblelog['name'];
  98.         $response['name'] = (string) $xml->tumblelog['name'];
  99.  
  100.         $response['email'] = '';
  101.  
  102.         $response['avatar'] = (string) $xml->tumblelog['avatar-url'];
  103.         $response['location'] = $tumblrInfo->location;
  104.         $response['desc'] = $tumblrInfo->description;
  105.         $response['url'] = (string) $xml->tumblelog['avatar-url'];
  106.  
  107.         $response['oauth_token'] = $token['oauth_token'];
  108.         $response['oauth_token_secret'] = $token['oauth_token_secret'];
  109.         $response['uIdentifier'] = (string) $xml->tumblelog['name'];
  110.  
  111.         return $response;        
  112.     }
  113.    
  114.     // In this function is the quizz, we retrieve permanent tokens from DB and set it on the oauth object.
  115.     function AutoShareThis($uId, $data) {
  116.         //We redefine the tumblrOauthObject because we need to change the OAUTH_AUTH_TYPE_URI for OAUTH_AUTH_TYPE_FORM, because the write requests
  117.         //must pass the parameters trough POST
  118.         $this->tumblrOauthObject = new OAuth($this->consumer_key,$this->consumer_secret,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_FORM);
  119.        
  120.         if ($dbtokens=$this->GetDBAccessTokens(0, $uId)) {
  121.             // We use a Try/catch structure for handle possible revoked or non existent tokenns.
  122.             try {
  123.                 //Here we set the permanent tokens previously retrieved from database.
  124.                 $this->tumblrOauthObject->setToken($dbtokens['oauth_token'], $dbtokens['oauth_token_secret']);
  125.                 $message = sprintf(T_("I've just found this awesome picture on %s: %s"), $GLOBALS['sitename'], $data['link']);
  126.                 //Seeking int he tumblr api docs, we can see the parameters we need to pass for the Oauth fetch call.
  127.                
  128.                 $params = array(
  129.                     'oauth_token' => $dbtokens['oauth_token'],
  130.                     'type'      => 'photo',                
  131.                     'source'    => $data['picture'],
  132.                     'url'       => $data['link'],
  133.                     'caption'      => $message,
  134.                     'click-through-url' =>$data['link'],
  135.                     'generator' => 'Testing example'
  136.                 );
  137.                
  138.                 $status=$this->tumblrOauthObject->fetch($this->write_url, $params);
  139.                 if ($status) $result=true;
  140.                
  141.             } catch(Exception $e) {
  142.                 //print_r($e);
  143.                 $result=false;
  144.                 $message='revoked';
  145.             }
  146.         } else {
  147.             $result=false;
  148.             $message='dberror';
  149.         }
  150.         return array(
  151.             'result'    => $result,
  152.             'message'   => $message
  153.         );
  154.     }
  155.    
  156.     function RequestLogout() {
  157.         session_destroy();
  158.     }
  159.    
  160. }
  161.  
  162. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement