twitter oAuth cURL示例不起作用 - 为什么?

时间:2010-07-20 19:01:19

标签: curl twitter oauth

  

可能重复:
  another twitter oAuth cURL access token request that fails

以下代码只是给了我:

无法验证oauth签名和令牌

        // Set url
        $url = "http://api.twitter.com/oauth/request_token";

        // Params to pass to twitter and create signature
        $params['oauth_callback'] = "http://localhost/twitter/tweet/";
        $params['oauth_consumer_key'] = $this->consumerKey;
        $params['oauth_nonce'] = SHA1(time());
        $params['oauth_timestamp'] = time();
        $params['oauth_signature_method'] = $this->signatureMethod;
        $params['oauth_version'] = $this->version;
        ksort($params);

        // Signing
            // Concatenating
            $concatenatedParams = '';
            foreach($params as $k => $v)
            {
              $k = urlencode($k);
              $v = urlencode($v);
              $concatenatedParams .= "{$k}={$v}&";
            }
            $concatenatedParams = urlencode(substr($concatenatedParams,0,-1));

            $signatureBaseString = "POST&".urlencode($url)."&".$concatenatedParams;         
            $base64Hashmac = base64_encode( hash_hmac('sha1', $signatureBaseString, $this->secret."&", true) );
            $params['oauth_signature'] = urlencode($base64Hashmac);


        // Do cURL
        $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,1);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
            $exec = curl_exec ($ch);
            $info = curl_getinfo($ch);
        curl_close ($ch);

        print $exec;

以下是从卷发打印出的信息$ info ...

Array
(
    [url] => http://api.twitter.com/oauth/request_token
    [content_type] => text/html; charset=utf-8
    [http_code] => 401
    [header_size] => 919
    [request_size] => 181
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 1.176
    [namelookup_time] => 0
    [connect_time] => 0.127
    [pretransfer_time] => 0.127
    [size_upload] => 934
    [size_download] => 44
    [speed_download] => 37
    [speed_upload] => 794
    [download_content_length] => 44
    [upload_content_length] => 934
    [starttransfer_time] => 0.127
    [redirect_time] => 0
    [request_header] => POST /oauth/request_token HTTP/1.1
Host: api.twitter.com
Accept: */*
Content-Length: 934
Content-Type: multipart/form-data; boundary=----------------------------7465678a46cc
)

1 个答案:

答案 0 :(得分:1)

您可能想要确切地说明它是如何工作的,事情可以通过许多不同的方式“不起作用”。你收到错误结果了吗?他们是什么,从哪个电话?是否有任何特定步骤失败?

首先,你不能使用“localhost”地址作为回调URL,如果twitter服务器连接到“localhost”,它只会连接到自身,而不是你。

此外,您构建$concatenatedParams的方式只会使您的$signatureBaseString部分进行网址编码。通常使用"{$k}%3D{$v}%26" {和=,然后在&urlencode添加到$concatenatedParams$signatureBaseString使用{{1}}。