Twitter请求令牌

时间:2014-08-13 15:37:16

标签: php twitter twitter-oauth

我正在尝试使用Twitter开发网站上的示例,但似乎无法获得与他们相同的签名。

我正在尝试完成https://dev.twitter.com/docs/auth/implementing-sign-twitter上的第3步,因为我收到错误“无效或过期的令牌”,但我知道这不是因为我刚刚获得它,所以它必须是某种东西我的数据包错了。

我用来尝试生成的代码是:

// testing bit
$oauth = array(
        'oauth_consumer_key'=>'cChZNFj6T5R0TigYB9yd1w',
        'oauth_nonce'=>'a9900fe68e2573b27a37f10fbad6a755',
        'oauth_signature_method'=>'HMAC-SHA1',
        'oauth_timestamp'=>'1318467427',
        'oauth_token'=>'NPcudxy0yU5T3tBzho7iCotZ3cnetKwcTIRlX0iwRl0',
        'oauth_version'=>'1.0'            
);            
$this->o_secret = 'LswwdoUaIvS8ltyTt5jkRh4J50vUPVVHtR2YPi5kE';
$this->c_secret = 'kAcSOqF21Fu85e7zjz7ZN2U4ZRhfV3WpwPAoE3Z7kBw';    

ksort($oauth);

$string = rawurlencode(http_build_query($oauth));

$new_string = strtoupper($http_method).'&'.rawurlencode($main_url[0]).'&'.$string;

// The request_token request doesn't need a o_secret because it doesn't have one!
$sign_key = strstr($fullurl,'request_token') ? $this->c_secret.'&' : $this->c_secret.'&'.$this->o_secret; 

echo urlencode(base64_encode(hash_hmac('sha1',$new_string,$sign_key,true)));exit;

我假设此页面上列出的密钥实际上是正确的:https://dev.twitter.com/docs/auth/creating-signature。因此,在这种情况下,签名应为39cipBtIOHEEnybAR4sATQTpl2I%3D。

如果你能发现我所缺少的那就太棒了。

3 个答案:

答案 0 :(得分:0)

page you reference您的消费者密码和令牌密钥不正确。如果你向上看页面,你会发现它们应该是:

Consumer secret: L8qq9PZyRg6ieKGEKhZolGC0vJWLw8iEJ88DRdyOg
Token secret: veNRnAWe6inFuo8o2u8SLLZLjolYDmDP7SzL0YfYI

同样在第3步中,您需要在计算签名基本字符串时在参数列表中包含oauth_verifier

我不熟悉PHP,所以我没有检查你的代码来计算签名。

答案 1 :(得分:0)

此代码现已有效 - 我将从那里整理它:)

    // This function is to help work out step 3 in the process and why it is failing
    public function testSignature(){
            // testing bit
            $oauth = array(
                    'oauth_consumer_key'=>'cChZNFj6T5R0TigYB9yd1w',
                    'oauth_nonce'=>'a9900fe68e2573b27a37f10fbad6a755',
                    'oauth_signature_method'=>'HMAC-SHA1',
                    'oauth_timestamp'=>'1318467427',
                    'oauth_token'=>'NPcudxy0yU5T3tBzho7iCotZ3cnetKwcTIRlX0iwRl0',
                    'oauth_version'=>'1.0'            
            );

            $this->o_secret = 'LswwdoUaIvS8ltyTt5jkRh4J50vUPVVHtR2YPi5kE';
            $this->c_secret = 'kAcSOqF21Fu85e7zjz7ZN2U4ZRhfV3WpwPAoE3Z7kBw';    

            ksort($oauth);

            $string = http_build_query($oauth);

            $new_string = strtoupper($http_method).'&'.$main_url[0].'&'.$string;

            $new_string = 'POST&https%3A%2F%2Fapi.twitter.com%2F1%2Fstatuses%2Fupdate.json&include_entities%3Dtrue%26oauth_consumer_key%3Dxvz1evFS4wEEPTGEFPHBog%26oauth_nonce%3DkYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1318622958%26oauth_token%3D370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb%26oauth_version%3D1.0%26status%3DHello%2520Ladies%2520%252B%2520Gentlemen%252C%2520a%2520signed%2520OAuth%2520request%2521';

            // The request_token request doesn't need a o_secret because it doesn't have one!
            $sign_key = $this->c_secret.'&'.$this->o_secret; 

            echo 'Should be: tnnArxj06cWHq44gCs1OSKk/jLY=<br>';
            echo 'We get: '.base64_encode(hash_hmac('sha1',$new_string,$sign_key,true));
            exit;    
    }

答案 2 :(得分:0)

您希望从Twitter访问令牌并登录您在此示例中可以看到的实现。

1)http://www.codexworld.com/login-with-twitter-using-php/

这个用于时间线推文

2)http://www.codexworld.com/create-custom-twitter-widget-using-php/

可能对你有帮助。