Tumblr API身份验证401错误

时间:2014-02-17 02:54:59

标签: zend-framework oauth tumblr xenforo

我正在努力解决这个问题,但是我正在做的一切我无法获取用户信息。我只是得到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 个答案:

答案 0 :(得分:1)

好吧,一切都很好,只是一个简单的错字......

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

应该是:

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