从https://api.twitter.com/1.1/statuses/user_timeline.json
中检索数据时,我在使用Twitter REST API和cURL方面遇到了一些问题我在下面进行了以下设置,它可以很好地运行,但是一旦我尝试获取扩展推文(tweet_mode =扩展),但用https://api.twitter.com/1.1/statuses/user_timeline.json?tweet_mode=extended
替换网址,就会出错并说Could not authenticate you.
并且如果我删除参数?tweet_mode=extended
它再次正常工作。
var $oauthConsumerKey = "XXX"; // Consumer key
var $oauthConsumerKeySecret = "XXX"; // Consumer key secret
var $oauthToken = "XXX"; // Token
var $oauthTokenSecret = "XXX"; // Token secret
$oauthHash = '';
$oauthHash .= 'oauth_consumer_key=' . $this->oauthConsumerKey . '&';
$oauthHash .= 'oauth_nonce=' . time() . '&';
$oauthHash .= 'oauth_signature_method=HMAC-SHA1&';
$oauthHash .= 'oauth_timestamp=' . time() . '&';
$oauthHash .= 'oauth_token=' . $this->oauthToken . '&';
$oauthHash .= 'oauth_version=1.0';
$base = '';
$base .= 'GET';
$base .= '&';
$base .= rawurlencode('https://api.twitter.com/1.1/statuses/user_timeline.json');
$base .= '&';
$base .= rawurlencode($oauthHash);
$key = '';
$key .= rawurlencode($this->oauthConsumerKeySecret);
$key .= '&';
$key .= rawurlencode($this->oauthTokenSecret);
$signature = base64_encode(hash_hmac('sha1', $base, $key, true));
$signature = rawurlencode($signature);
$oauthHeader = '';
$oauthHeader .= 'oauth_consumer_key="' . $this->oauthConsumerKey . '", ';
$oauthHeader .= 'oauth_nonce="' . time() . '", ';
$oauthHeader .= 'oauth_signature="' . $signature . '", ';
$oauthHeader .= 'oauth_signature_method="HMAC-SHA1", ';
$oauthHeader .= 'oauth_timestamp="' . time() . '", ';
$oauthHeader .= 'oauth_token="' . $this->oauthToken . '", ';
$oauthHeader .= 'oauth_version="1.0", ';
$curlHeader = array("Authorization: Oauth {$oauthHeader}", 'Expect:');
$curlRequest = curl_init();
curl_setopt($curlRequest, CURLOPT_HTTPHEADER, $curlHeader);
curl_setopt($curlRequest, CURLOPT_HEADER, false);
curl_setopt($curlRequest, CURLOPT_URL, 'https://api.twitter.com/1.1/statuses/user_timeline.json');
curl_setopt($curlRequest, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlRequest, CURLOPT_SSL_VERIFYPEER, false);
$this->twiiterJSON = curl_exec($curlRequest);
curl_close($curlRequest);
我在Postman中进行设置以检查并且在Postman中它工作正常所以我生成了一个PHP cURL代码片段并用它进行了测试它似乎工作正常所以任何人都可以帮我弄清楚我做错了什么?我认为这可能与签名有关,但如果没有参数,它似乎没问题。
任何帮助/建议都将不胜感激。
答案 0 :(得分:2)
而不是弄清楚什么是错的,我只会使用现在有用的东西
<?php
$username = REX_VALUE[1];
$key = REX_VALUE[2]; // Consumer Key (API Key)
$secret = REX_VALUE[3]; // Consumer Secret (API Secret)
$counter = '0'; // Zähler, wieviele Tweets ausgegeben werden sollen
setlocale(LC_TIME, "de_DE.utf8");
$quest = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=" . urlencode($username) . "&tweet_mode=extended&include_rts=false&exclude_replies=true";
$tokenUrl = "https://api.twitter.com/oauth2/token";
$auth = base64_encode(urlencode($key) . ':' . urlencode($secret));
// BearerToken holen
$getToken = curl_init();
curl_setopt($getToken, CURLOPT_URL, $tokenUrl);
curl_setopt($getToken, CURLOPT_POST, 1);
curl_setopt($getToken, CURLOPT_HTTPHEADER, array('Authorization: Basic ' . $auth));
curl_setopt($getToken, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
curl_setopt($getToken, CURLOPT_RETURNTRANSFER, 1);
$token = json_decode(curl_exec($getToken));
$token = $token->access_token;
curl_close($getToken);
// Tweets holen
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $quest);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $token));
$result = curl_exec($curl);
curl_close($curl);
$tweets = json_decode($result);