2014-02-17 103 views
0

我正在努力弄清楚這一點,但對於我所做的一切,我無法獲取用戶信息。我只是得到一個401錯誤,不知道爲什麼。 代碼:Tumblr API身份驗證401錯誤

<?php 
class Tumblr 
{ 
    public function connect() 
    { 
     $tumblr = new ExternalExtended_Helper_Tumblr; 

     $oauth = $tumblr->getOauthConsumer('https://mysite.com/register'); 
     $requestToken = $oauth->getRequestToken(); 

     $accessToken = $oauth->getAccessToken($this->_input->filter(array(
      'oauth_token' => XenForo_Input::STRING, 
      'oauth_verifier' => XenForo_Input::STRING 
     )), $requestToken); 

     $user = $tumblr->retrieveUserInfo($accessToken); 
    } 
} 

<?php 

class Helper_Tumblr 
{ 
    /** 
    * Returns a reference to the OAuth consumer, instantiating it if necessary 
    * 
    * @param string $callbackUrl URL to return to 
    * 
    * @return bool|Zend_Oauth_Consumer False if no Tumblr app configured, otherwise Oauth consumer 
    */ 
    public static function getOauthConsumer($callbackUrl = '') 
    { 
     $options = XenForo_Application::getOptions(); 

     if (!$options->tumblrAppKey || !$options->tumblrAppSecret) 
     { 
      return false; 
     } 

     return new Zend_Oauth_Consumer(array(
      'callbackUrl' => $callbackUrl, 
      'requestTokenUrl' => 'https://www.tumblr.com/oauth/request_token', 
      'authorizeUrl' => 'https://www.tumblr.com/oauth/authorize', 
      'accessTokenUrl' => 'https://www.tumblr.com/oauth/access_token', 
      'consumerKey' => $options->tumblrAppKey, 
      'consumerSecret' => $options->tumblrAppSecret, 
     )); 
    } 

    /** 
    * Returns a reference to the OAuth client, instantiating it if necessary 
    * 
    * @param string $accessToken Tumblr access token (from code swap, or given by user); may be empty 
    * 
    * @return bool|Zend_Oauth_Client False if no Tumblr app configured, otherwise Oauth client 
    */ 
    public static function getOauthClient($accessToken) 
    { 
     $options = XenForo_Application::getOptions(); 

     if (!$options->tumblrAppKey || !$options->tumblrAppSecret) 
     { 
      return false; 
     } 

     $access = new Zend_Oauth_Token_Access(); 

     $access->setToken($accessToken->getToken()); 
     $access->setTokenSecret($accessToken->getToken()); 

     return $access->getHttpClient(array(
      'consumerKey' => $options->tumblrAppKey, 
      'consumerSecret' => $options->tumblrAppSecret 
     )); 
    } 

    /** 
    * Get a User's Information 
    * 
    * @return json 
    */ 
    public static function retrieveUserInfo($accessToken) 
    { 
     $oauthClient = self::getOauthClient($accessToken); 
     $oauthClient->setUri('http://api.tumblr.com/v2/user/info'); 

     $response = $oauthClient->request(Zend_Http_Client::POST); 

     if ($response->isError()) 
     { 
      throw new Exception("An error occurred sending request. Status code: {$response->getStatus()}"); 
     } 

     return $response; 
    } 
} 

在功能「retrieveUserInfo」和對象的轉儲出現的錯誤是這樣的:

object(Zend_Http_Response)#280 (5) { 
    ["version":protected] => string(3) "1.1" 
    ["code":protected] => int(401) 
    ["message":protected] => string(14) "Not Authorized" 
    ["headers":protected] => array(7) { 
    ["Server"] => string(5) "nginx" 
    ["Date"] => string(29) "Mon, 17 Feb 2014 02:53:08 GMT" 
    ["Content-type"] => string(31) "application/json; charset=utf-8" 
    ["Transfer-encoding"] => string(7) "chunked" 
    ["Connection"] => string(5) "close" 
    ["Set-cookie"] => string(89) "tmgioct=53017993ddbda801421421421421; expires=Thu, 15-Feb-2024 02:53:07 GMT; path=/; httponly" 
    ["P3p"] => string(46) "CP="ALL ADM DEV PSAi COM OUR OTRo STP IND ONL"" 
    } 
    ["body":protected] => string(71) "3c 
{"meta":{"status":401,"msg":"Not Authorized"},"response":[]} 
0 

" 

爲什麼說我沒有被授權,什麼都沒有我完成了授權?

謝謝!

回答

1

好吧,這一切都很好,只是一個簡單的拼寫錯誤...

$access->setToken($accessToken->getToken()); 
$access->setTokenSecret($accessToken->getToken()); 

應該是:

$access->setToken($accessToken->getToken()); 
$access->setTokenSecret($accessToken->getTokenSecret());