Reputation: 157
I've been using Abraham Williams Twitter OAuth Library and it was a breeze setting everything up, except for one thing. I try to post a message to twitter and get a 401 not authorized. I've been searching the Internet for answers, I tried getting new Consumer keys but nothing seems to work.
This is the code I'm using to post a test tweet to 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!";
}
And this is the error I get. I don't have a Callback URL. Should I have one?
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
)
)
Upvotes: 1
Views: 866
Reputation: 157
I fixed it using another great library from 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
I used this code to post to 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();
Upvotes: 2