【问题标题】:Twitter: How can I get all tweets in json format using twitter apiTwitter:如何使用 twitter api 获取 json 格式的所有推文
【发布时间】:2014-10-23 03:12:59
【问题描述】:

如何使用 twitter api 获取所有 json 格式的推文?我已经检查了下面链接中给出的过程:

https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline

但出现错误:{"errors":[{"message":"Bad Authentication data","code":215}]}

谁能建议我该怎么做。我不想使用嵌入式时间线小部件。我只想以我的个人格式显示来自我帐户的推文。

【问题讨论】:

    标签: twitter


    【解决方案1】:

    问题在于您的身份验证方法 - API 认为您传递了错误的身份验证数据。我不确定你是怎么做的,但这篇文章更多地解释了你遇到的问题:

    https://dev.twitter.com/discussions/11589

    在尝试获取推文之前,您应该首先关注身份验证并确保其正常工作。如果您有任何其他问题,请随时澄清:)

    【讨论】:

      【解决方案2】:

      我使用了下面列出的示例代码:

      $token = 'xxxxxx';
      $token_secret = 'xxxxxx';
      $consumer_key = 'xxxxxx';
      $consumer_secret = 'xxxxxx';
      
      $host = 'api.twitter.com';
      $method = 'GET';
      $path = '/1.1/statuses/user_timeline.json'; // api call path
      
      $query = array( // query parameters
      'screen_name' => 'twitterapi',
      'count' => '10'
      );
      
      $oauth = array(
      'oauth_consumer_key' => $consumer_key,
      'oauth_token' => $token,
      'oauth_nonce' => (string)mt_rand(), // a stronger nonce is recommended
      'oauth_timestamp' => time(),
      'oauth_signature_method' => 'HMAC-SHA1',
      'oauth_version' => '1.0'
      );
      
      $oauth = array_map("rawurlencode", $oauth); // must be encoded before sorting
      $query = array_map("rawurlencode", $query);
      
      $arr = array_merge($oauth, $query); // combine the values THEN sort
      
      asort($arr); // secondary sort (value)
      ksort($arr); // primary sort (key)
      
      // http_build_query automatically encodes, but our parameters
      // are already encoded, and must be by this point, so we undo
      // the encoding step
      $querystring = urldecode(http_build_query($arr, '', '&'));
      
      $url = "https://$host$path";
      
      // mash everything together for the text to hash
      $base_string = $method."&".rawurlencode($url)."&".rawurlencode($querystring);
      
      // same with the key
      $key = rawurlencode($consumer_secret)."&".rawurlencode($token_secret);
      
      // generate the hash
      $signature = rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $key, true)));
      
      // this time we're using a normal GET query, and we're only encoding the query params
      // (without the oauth params)
      $url .= "?".http_build_query($query);
      $url=str_replace("&","&",$url); //Patch by @Frewuill
      
      $oauth['oauth_signature'] = $signature; // don't want to abandon all that work!
      ksort($oauth); // probably not necessary, but twitter's demo does it
      
      // also not necessary, but twitter's demo does this too
      function add_quotes($str) { return '"'.$str.'"'; }
      $oauth = array_map("add_quotes", $oauth);
      
      // this is the full value of the Authorization line
      $auth = "OAuth " . urldecode(http_build_query($oauth, '', ', '));
      
      // if you're doing post, you need to skip the GET building above
      // and instead supply query parameters to CURLOPT_POSTFIELDS
      $options = array( CURLOPT_HTTPHEADER => array("Authorization: $auth"),
                    //CURLOPT_POSTFIELDS => $postfields,
                    CURLOPT_HEADER => false,
                    CURLOPT_URL => $url,
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_SSL_VERIFYPEER => false);
      
      // do our business
      $feed = curl_init();
      curl_setopt_array($feed, $options);
      $json = curl_exec($feed);
      curl_close($feed);
      
      $twitter_data = json_decode($json);
      

      最后我得到了一组结果集。

      【讨论】:

        猜你喜欢
        • 2014-02-02
        • 1970-01-01
        • 2012-12-12
        • 2015-05-15
        • 2017-07-14
        • 2018-03-16
        • 2023-03-31
        • 2022-12-22
        • 2016-05-18
        相关资源
        最近更新 更多