wali
wali

Reputation: 477

Trouble using PHP Twitter libraries to send tweet

To preface, I don't know much about PHP. However, I'm not sure that this is a PHP issue but maybe more of an issue with me understanding the libraries for connecting to Twitter.

I followed the tutorial here http://140dev.com/twitter-api-programming-tutorials/hello-twitter-oauth-php/ which uses Matt Harris' twitter library. I didn't change anything except the keys which I obtained from twitter.

When I run the following code I get an unauthorized error code 401:

  $tweet_text = 'Hello Twitter';
  print "Posting...\n";
  $result = post_tweet($tweet_text);
  print "Response code: " . $result . "\n";

 function post_tweet($tweet_text) {

 require_once('tmhoauth/tmhOAuth.php');

 $connection = new tmhOAuth(array(
'consumer_key' => '******',
'consumer_secret' => '******',
'user_token' => '******',
'user_secret' => '******',
)); 

// Make the API call
$connection->request('POST', 
$connection->url('1/statuses/update'), 
array('status' => $tweet_text));

return $connection->response['code'];
 }
?>

As I said previously, I am using the keys provided by twitter.

Could someong help me with trying to use the above library to test the twitter credientail validator at https://api.twitter.com/1/account/verify_credentials.json?

I also tried using the following example with the TwitterOAuth library from this SO post Using basic oauth to send a tweet:

<?php
require_once('twitteroauth.php');
$connection = new TwitterOAuth('app consumer key', 'app consumer secret', 'my access     token', 'my access token secret');
$connection->post('statuses/update', array('status' => 'text to be tweeted'));

It didn't work as well; but it did forward the page to the author's github page!?!?

Thanks for any help you can provide!

Upvotes: 0

Views: 592

Answers (1)

Sharpedges
Sharpedges

Reputation: 405

If you are sure the keys are correct I would advise you check the application settings within twitter to ensure that write mode is enabled.

Under the applications settings make sure the application type access is set to

Read, Write and Access direct messages

(Or at least Read/Write)

Then perhaps try generating the access token again?

Upvotes: 2

Related Questions