我一直在使用亚伯拉罕·威廉姆斯推特OAuth图书馆,除了一件事之外,一切都很简单。我尝试将消息发布到Twitter并获得未经授权的401。我一直在互联网上寻找答案,我尝试获得新的消费者密钥,但似乎没有任何效果。
这是我用来向Twitter发布测试推文的代码。
// Connection
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET);
// Posten
$connection->post('statuses/update', array('status' => "Dit is een tweet via de Twitter API"));
// Error afhandeling
$httpc = $connection->http_code;
if($httpc == 200) {
echo 'Tweet posted!';
} else {
echo "Fail!";
}
这就是我得到的错误。我没有回拨网址。我应该买一个吗?
TwitterOAuth Object
(
[http_code] => 401
[url] => https://api.twitter.com/1.1/statuses/update.json
[host] => https://api.twitter.com/1.1/
[timeout] => 30
[connecttimeout] => 30
[ssl_verifypeer] =>
[format] => json
[decode_json] => 1
[http_info] => Array
(
[url] => https://api.twitter.com/1.1/statuses/update.json
[content_type] => application/json; charset=utf-8
[http_code] => 401
[header_size] => 918
[request_size] => 519
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.652405
[namelookup_time] => 0.006853
[connect_time] => 0.108722
[pretransfer_time] => 0.330628
[size_upload] => 331
[size_download] => 107
[speed_download] => 164
[speed_upload] => 507
[download_content_length] => 107
[upload_content_length] => 331
[starttransfer_time] => 0.652388
[redirect_time] => 0
[certinfo] => Array
(
)
[redirect_url] =>
)
[useragent] => TwitterOAuth v0.2.0-beta2
[sha1_method] => OAuthSignatureMethod_HMAC_SHA1 Object
(
)
[consumer] => OAuthConsumer Object
(
[key] => xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[secret] => xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[callback_url] =>
)
[token] => OAuthConsumer Object
(
[key] => xxxxxxxxxxxxxxxxxxxxxxxxx
[secret] => xxxxxxxxxxxxxxxxxxxxxxxx
[callback_url] =>
)
[http_header] => Array
(
[cache_control] => no-cache, max-age=300
[content_length] => 107
[content_type] => application/json; charset=utf-8
[date] => Wed, 31 Jul 2013 07:03:19 GMT
[expires] => Wed, 31 Jul 2013 07:08:19 GMT
[server] => tfe
[set_cookie] => guest_id=v1%3A137525419888158376; Domain=.twitter.com; Path=/; Expires=Fri, 31-Jul-2015 07:03:19 UTC
[status] => 401 Unauthorized
[strict_transport_security] => max-age=631138519
[vary] => Accept-Encoding
[www_authenticate] => OAuth realm="https://api.twitter.com"
[x_frame_options] => SAMEORIGIN
[x_transaction] => 8d1700c605af39d5
)
)
答案 0 :(得分:2)
我使用J7mbo的另一个很棒的库来修复它 https://github.com/J7mbo/twitter-api-php/blob/master/README.md
Simplest PHP example for retrieving user_timeline with Twitter API version 1.1
我使用此代码发布到Twitter。
/** URL for REST request, see: https://dev.twitter.com/docs/api/1.1/ **/
$url = 'https://api.twitter.com/1.1/statuses/update.json';
$requestMethod = 'POST';
$postfields = array(
'status' => 'Test'
);
/** Perform the request and echo the response **/
$twitter = new TwitterAPIExchange($settings);
echo $twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();