donglian1384 2012-09-07 06:45
浏览 31
已采纳

使用tmhOAuth时,Twitter api返回状态400

I have the following code:

error_reporting(E_ALL);

 ini_set("display_errors", 1);

$db = mysqli_connect("db.mysql","user","pwd","db");

$consumersecret = "Cy4qwsC1fdvP62x------BiEQMLwbDUUA";
$consumerkey = "0XCjdxL------lJc---UtA";

$accesstoken = "154823199------QO4L8xfMgpsGHLb-----xTOuUf4WMwY";
$accesssecret = "mOwRu6b34--------------ZRktk3pg";

require_once('tmhOAuth.php');
    $connection = new tmhOAuth(array(
       'consumer_key' => $consumersecret,
        'consumer_secret' => $consumerkey,
        'user_token' => $accesstoken,
        'user_secret' => $accesssecret,
      )); 

  $code=$connection->request('GET', $connection->url('1/statuses/user_timeline'),

                array('include_entities' => 'true', 

                        'include_rts' => 'true',

                        'screen_name'=>'mdibbets',

                        'count'=>'2'),true);



        if ($code == 200) {

            $responseData = json_decode($connection->response['response'],true);

            var_dump($connection->response['response']);

            }

        var_dump($connection->response);

The response I am getting is the following:

array(4) {
  ["headers"]=>
  array(12) {
    ["x_ratelimit_limit"]=>
    string(3) "150"
    ["x_ratelimit_remaining"]=>
    string(1) "0"
    ["x_ratelimit_reset"]=>
    string(10) "1347001832"
    ["x_ratelimit_class"]=>
    string(3) "api"
    ["content_type"]=>
    string(30) "application/json;charset=utf-8"
    ["set_cookie"]=>
    string(99) "guest_id="v1:134699975230464174"; Expires=Sun, 7-Sep-2014 06:35:52 GMT; Path=/; Domain=.twitter.com"
    ["status"]=>
    string(15) "400 Bad Request"
    ["x_transaction"]=>
    string(16) "ba0517c8728574ad"
    ["x_frame_options"]=>
    string(10) "SAMEORIGIN"
    ["date"]=>
    string(29) "Fri, 07 Sep 2012 06:35:52 GMT"
    ["content_length"]=>
    string(3) "200"
    ["server"]=>
    string(3) "tfe"
  }
  ["code"]=>
  int(400)
  ["response"]=>
  string(200) "{"request":"\/1\/statuses\/user_timeline.json?count=2&include_entities=true&include_rts=true&screen_name=mdibbets","error":"Rate limit exceeded. Clients may not make more than 150 requests per hour."}"
  ["info"]=>
  array(23) {
    ["url"]=>
    string(121) "https://api.twitter.com/1/statuses/user_timeline.json?count=2&include_entities=true&include_rts=true&screen_name=mdibbets"
    ["content_type"]=>
    string(30) "application/json;charset=utf-8"
    ["http_code"]=>
    int(400)
    ["header_size"]=>
    int(450)
    ["request_size"]=>
    int(529)
    ["filetime"]=>
    int(-1)
    ["ssl_verify_result"]=>
    int(0)
    ["redirect_count"]=>
    int(0)
    ["total_time"]=>
    float(0.743177)
    ["namelookup_time"]=>
    float(3.7E-5)
    ["connect_time"]=>
    float(0.184139)
    ["pretransfer_time"]=>
    float(0.555495)
    ["size_upload"]=>
    float(0)
    ["size_download"]=>
    float(200)
    ["speed_download"]=>
    float(269)
    ["speed_upload"]=>
    float(0)
    ["download_content_length"]=>
    float(200)
    ["upload_content_length"]=>
    float(0)
    ["starttransfer_time"]=>
    float(0.743047)
    ["redirect_time"]=>
    float(0)
    ["certinfo"]=>
    array(0) {
    }
    ["redirect_url"]=>
    string(0) ""
    ["request_header"]=>
    string(529) "GET /1/statuses/user_timeline.json?count=2&include_entities=true&include_rts=true&screen_name=mdibbets HTTP/1.1
User-Agent: themattharris' HTTP Client
Host: api.twitter.com
Accept: */*
Authorization: OAuth oauth_consumer_key="Cy4qwsC1fdvP6--------EQMLwbDUUA", oauth_nonce="0a119b791------604faaf23", oauth_signature="OsHfGZ------wmMApoxNdddD4%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1346999751", oauth_token="154823-------gpsGHLb1DMUnxxTOuUf4WMwY", oauth_version="1.0"

"
  }
}

Is there some trick to get the Oauth working that I didn't use? Somehow all documentation of tmoauth results in a 404, but it's heavely recommended.

Could someone please give me a pointer how to correctly use tmoauth? Documentation, example code, anything?

I'm using a shared virtual server host, so twitter obviously recognises the IP from other clients of my hosting company.

If someone knows a well documented way to do this without tmoauth, purely native php I'm all ears, but it really needs good documentation.

  • 写回答

1条回答 默认 最新

  • duanfu7004 2012-09-14 05:32
    关注
    • tmhOauth has its documentation inside examples. Examples are very straightforward and beneficial to understand the code. You can find examples here:
      https://github.com/themattharris/tmhOAuth-examples

    • You can get your own application's access tokens also and use your own tokens. Check this: https://dev.twitter.com/discussions/631

    • Regarding to your code in question, you get 400 error, it's different from 404. You made a bad request. From the headers I can see that you consumed your rate limit. You should wait until next reset time. Which is epoch 1347001832 = Fri, 07 Sep 2012 07:10:32 GMT

    As a side comment, you can use Twitter API 1.1 which is presented last week. Which gives more rate limit.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大