使用Codeigniter请求OAuth访问凭据时出现401错误

时间:2012-01-24 21:56:44

标签: php codeigniter oauth

因此,我正在整理一个快速而肮脏的应用程序,根据从RSS源提取的新项目自动向Tumblr添加新帖子。该应用程序位于Codeigniter中,到目前为止,我已设法获取请求凭据并将用户发送给Tumblr进行授权。问题是,一旦他们被重定向回应用程序并且我请求访问凭据,我就会收到以下错误:

Message: file_get_contents(http://www.tumblr.com/oauth/access_token?oauth_consumer_key=THECONSUMERKEY&oauth_nonce=9362afdd34f9ce1601fb9cf505ffa3cf&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1327440390&oauth_token=09mFsxCvsODDmSxPCyQNu4QKFMMXaAEEyPtBibPUyUTE1n2BsJ&oauth_verifier=hDfGgesf9EKIO5yFhiHxtnsbP42XEP1FISY2qyyWerzXf6fPTG&oauth_version=1.0&oauth_signature=yeFw8ACvVvKtD%2BQ%2FdzbLivDSm1Y%3D) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized

这是我正在使用的代码:

$oauthVerifier = $_GET["oauth_verifier"];
        $sigBase = "GET&" . rawurlencode($this->accessTokenUrl) . "&"
                . rawurlencode("oauth_consumer_key=" . rawurlencode($this->consumerKey)
                        . "&oauth_nonce=" . rawurlencode($this->nonce)
                        . "&oauth_signature_method=" . rawurlencode($this->oauthSignatureMethod)
                        . "&oauth_timestamp=" . $this->oauthTimestamp
                        . "&oauth_token=" . rawurlencode($this->CI->session->userdata('requestToken'))
                        . "&oauth_verifier=" . rawurlencode($oauthVerifier)
                        . "&oauth_version=" . $this->oauthVersion);
        $sigKey = $this->consumerSecret . "&";
        $oauthSig = base64_encode(hash_hmac("sha1", $sigBase, $sigKey, true));

        $requestUrl = $this->accessTokenUrl . "?"
                . "oauth_consumer_key=" . rawurlencode($this->consumerKey)
                . "&oauth_nonce=" . rawurlencode($this->nonce)
                . "&oauth_signature_method=" . rawurlencode($this->oauthSignatureMethod)
                . "&oauth_timestamp=" . rawurlencode($this->oauthTimestamp)
                . "&oauth_token=" . rawurlencode($this->CI->session->userdata('requestToken'))
                . "&oauth_verifier=" . rawurlencode($oauthVerifier)
                . "&oauth_version=" . rawurlencode($this->oauthVersion)
                . "&oauth_signature=" . rawurlencode($oauthSig);

        $response = file_get_contents($requestUrl);

有什么好主意吗?

2 个答案:

答案 0 :(得分:1)

这是不完整的,需要你追逐一些bug。

了解对话的相关部分:

TumApp to user: please go to Tumblr with one of my request tokens: GHF3F4F

user to Tumblr: I authorize TumApp, and here is it's request token: GHF3F4F

Tumblr to itself: let me authorize GHF3F4F, which TumApp can exchange for an access token
       to user: redirect to Tumapp's callback

Tumapp to itself: Great, user authorized me (by requesting my callback). Let me exchange my request token for an access token
       to Tumblr: Hey, give me an access token for GHF3F4F

Tumblr to Tumapp: Epic 401 fail!

以下是Tumblr给出401的原因

来自http://oauth.net/core/1.0a/#http_codes

HTTP 401 Unauthorized
    Invalid Consumer Key
    Invalid / expired Token
    Invalid signature
    Invalid / used nonce

我会从令牌开始推断;使用者密钥成功获得了请求令牌,并且库生成了签名和随机数。

答案 1 :(得分:0)

UPDATE :迷路&符号实际上不在原始代码中(我的错误),但我在尝试修复它之后做了些什么。事实上,原始问题的原因是签名密钥中没有请求令牌密钥。

感谢指针aitchnyu - 最后,问题是一个流浪的&符号。在签名库中,我在“oauth_consumer_key =”之前将&符号包裹在rawurlencode中,当它应该在它之前。

这是旧的sig基地:

"GET&" . rawurlencode($this->accessTokenUrl)
                . rawurlencode("&oauth_consumer_key=" . rawurlencode($this->consumerKey)
                        . "&oauth_nonce=" . rawurlencode($this->nonce)
                        . "&oauth_signature_method=" . rawurlencode($this->oauthSignatureMethod)
                        . "&oauth_timestamp=" . rawurlencode($this->time)
                        . "&oauth_token=" . rawurlencode($token)
                        . "&oauth_verifier=" . rawurlencode($oauthVerifier)
                        . "&oauth_version=" . $this->oauthVersion);

新的:

"GET&" . rawurlencode($this->accessTokenUrl) . "&"
                . rawurlencode("oauth_consumer_key=" . rawurlencode($this->consumerKey)
                        . "&oauth_nonce=" . rawurlencode($this->nonce)
                        . "&oauth_signature_method=" . rawurlencode($this->oauthSignatureMethod)
                        . "&oauth_timestamp=" . rawurlencode($this->time)
                        . "&oauth_token=" . rawurlencode($token)
                        . "&oauth_verifier=" . rawurlencode($oauthVerifier)
                        . "&oauth_version=" . $this->oauthVersion);

生命的差异!