0

I'm trying to set up a javascript function to post a status to a twitter account using POST statuses/update, details here: https://dev.twitter.com/docs/api/1/post/statuses/update. The goal is a Twitter post similar to the open graph actions on Facebook.

I'm using jQuery ajax to make the post request, here's what I have so far:

$.ajax
({
  type: "POST",
  url: "https://api.twitter.com/1/statuses/update.json",
  headers: jsonData,
  data: {},
  dataType: "jsonp",
  success: function( data )
  {

  }
});

I believe that I need to generate a header something like this for security:

Authorization: OAuth oauth_consumer_key=consumerKey, oauth_nonce=nonce, 
oauth_signature=signature, oauth_signature_method="HMAC-SHA1",  
oauth_timestamp=timestamp, oauth_token=userToken, oauth_version="1.0"

I have the consumer key for my app, I can generate a nonce, I'm generating the signature and timestamp using the methods from this question Twitter OAuth authentication in javascript. The only thing I have left is th oauth_token, which I believe is the token of the user whose feed I wish to post to. Please correct me if I'm wrong about that.

The problem is, I have absolutely no idea how to get this token from the user in order to post to their feed. I've spent the last 2 hours running around in circles through Twitter's oAuth documention without finding anything that looked useful; everything I've found was either flowcharts with no code examples or predicated on my code already having the user's oAuth token.

My question is this: how can I get the logged in user's oAuth token using javascript?

If that is not possible, I have another page where I am currently storing the user's twitter id in the database with their permission, getting their token and databasing it in PHP would also be satisfactory, assuming it doesn't change very frequently.

Community
  • 1
  • 1
user1623484
  • 23
  • 1
  • 7

2 Answers2

1

In order to obtain the oauth_token you need to follow the authentication process. Your application needs to be authorized to act on the behalf of the user. I would recomend to take some time first and learn how OAuth exactly works (there is a lot of information available) and then implement it in your app. (http://hueniverse.com/oauth/) You could also benefit from a library which will make your life easier. (in your case, look at: https://dev.twitter.com/docs/twitter-libraries#php).

Hope this has been useful.

AnduA
  • 610
  • 1
  • 6
  • 10
  • I'm somewhat familiar with how the oAuth system works, my problem lies with Twitter's api. I know that I need to have a user token so that when I make the post request Twitter's system knows who to associate the tweet with. I just have no idea how to get the user token because in all of those docs, twitter simply uses flowcharts. The closest thing I've found to code is https://dev.twitter.com/docs/auth/implementing-sign-twitter I'll go through that and see if I can find anything, though it would be really nice if they had a code example somewhere. – user1623484 Aug 24 '12 at 20:29
  • The token is returned via a callback, made by Twitter to your app. Everything you need to know is in that link. Also, try to use one of the php libraries, they might help you obtain the access token faster than reading and understanding the whole stuff. – AnduA Aug 24 '12 at 20:52
1

Here is example for get twitter oauth token and post tweet in twitter . Code sample is in php .

http://www.phpgang.com/how-to-post-tweet-on-twitter-with-php_414.html

1) to obtain the oauth token you need to follow the authentication process.

2) and your application needs to be authorized to act on the behalf of the user.

you can also see this twitter example for better understanding how it works

In this use can see the process of authorized user and post and get json result.

https://dev.twitter.com/rest/tools/console

I hope this will help you.

thanks

Neha Thakur
  • 351
  • 1
  • 12
  • 37