donglian1384
2012-09-07 06:45 阅读 28

使用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 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.

    点赞 评论 复制链接分享

相关推荐