1

我正在尝试使用 The Matt Harris 的 oAuth php 库从我的应用程序上的 Twitter 用户那里获取用户访问令牌,但我不断收到错误“无法验证 oauth 签名和令牌”。

具体来说,我正在尝试使用他的 OAuth 流程教程的略微修改版本来获取他们的令牌并将它们存储在我的数据库中而不是会话中。https://github.com/themattharris/tmhOAuth/blob/master/examples/oauth_flow.php 这里的request token函数总是出现错误。

function request_token($tmhOAuth) {
    $code = $tmhOAuth->request(
       'POST',
       $tmhOAuth->url('oauth/request_token', ''),
       array(
           'oauth_callback' => tmhUtilities::php_self()
       )
    );

    if ($code == 200) {
        $_SESSION['oauth'] = $tmhOAuth->extract_params($tmhOAuth->response['response']);
       authorize($tmhOAuth);
    } else {
       outputError($tmhOAuth);
    }
}

我已经检查了服务器时间,我知道它在 GMT 5 秒内。一位 Twitter 开发人员说时间应该在他们服务器的大约 5 分钟内,所以我应该没问题。https://dev.twitter.com/discussions/1043

我还检查了该cacert.pem文件是否与其他文件位于同一位置,就在 CURL 期望的位置,并且我关闭了“use_ssl”和“curl_ssl_verifypeer”选项,因为我的网站目前没有SSL 证书设置。

这是返回的响应对象:

["response"]=> array(6) { 
        ["headers"]=> array(16) { 
            ["date"]=> string(29) "Sat, 25 Aug 2012 16:38:24 GMT" 
            ["status"]=> string(16) "401 Unauthorized" 
            ["x_frame_options"]=> string(10) "SAMEORIGIN" 
            ["x_mid"]=> string(40) "4147a367997b7a5221cbcd8446652b1ebc00230d" 
            ["last_modified"]=> string(29) "Sat, 25 Aug 2012 16:38:24 GMT" 
            ["content_type"]=> string(24) "text/html; charset=utf-8" 
            ["expires"]=> string(29) "Tue, 31 Mar 1981 05:00:00 GMT" 
            ["cache_control"]=> string(62) "no-cache, no-store, must-revalidate, pre-check=0, post-check=0" 
            ["x_runtime"]=> string(7) "0.01766" 
            ["pragma"]=> string(8) "no-cache" 
            ["x_transaction"]=> string(16) "b8ffe07df233ba05" 
            ["set_cookie"]=> string(265) "_twitter_sess=ginormous session id; domain=.twitter.com; path=/; HttpOnly" 
            ["vary"]=> string(15) "Accept-Encoding" 
            ["content_encoding"]=> string(4) "gzip" 
            ["content_length"]=> string(2) "62" 
            ["server"]=> string(3) "tfe"
        } 
        ["code"]=> int(401) 
        ["response"]=> string(44) "Failed to validate oauth signature and token" 
        ["info"]=> array(23) { 
            ["url"]=> string(42) "http://api.twitter.com/oauth/request_token" 
            ["content_type"]=> string(24) "text/html; charset=utf-8" 
            ["http_code"]=> int(401) ["header_size"]=> int(1030) 
            ["request_size"]=> int(498) ["filetime"]=> int(-1) 
            ["ssl_verify_result"]=> int(0) 
            ["redirect_count"]=> int(0) 
            ["total_time"]=> float(0.265656) 
            ["namelookup_time"]=> float(0.062211) 
            ["connect_time"]=> float(0.139025) 
            ["pretransfer_time"]=> float(0.139028) 
            ["size_upload"]=> float(0) 
            ["size_download"]=> float(62) 
            ["speed_download"]=> float(233) 
            ["speed_upload"]=> float(0) 
            ["download_content_length"]=> float(62) 
            ["upload_content_length"]=> float(-1) 
            ["starttransfer_time"]=> float(0.265504) 
            ["redirect_time"]=> float(0) 
            ["certinfo"]=> array(0) { } 
            ["redirect_url"]=> string(0) "" 
            ["request_header"]=> string(498) "POST /oauth/request_token HTTP/1.1 User-Agent: tmhOAuth 0.621-SSL - //github.com/themattharris/tmhOAuth Host: api.twitter.com Accept: */* Accept-Encoding: deflate, gzip Authorization: OAuth oauth_callback="http%3A%2F%2Fcompleteset.us%2Fsettings%2Ftwitterlink", oauth_consumer_key="consumer_key", oauth_nonce="nonce", oauth_signature="sig", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1345912704", oauth_version="1.0" " 
        } 
        ["error"]=> string(0) "" 
        ["errno"]=> int(0) 
    }  

我注意到,在我将“use_ssl”设置为 false 之前,响应中的 headers 数组不存在。我不确定这意味着什么,或者它是否有帮助,但我想我不妨把它扔掉。

如果您需要任何其他部分,我还会转储 tmhOAuth 对象的所有其余部分。

4

0 回答 0