开发者

Getting started with Twitter\OAuth2\Python

I'm attempting to connect to twitter using python, and I'm finding it really frustrating.
Everything I read suggests that I need a consumer key, a consumer secret, an access key and an access secret - for example: Using python OAUTH2 to access OAUTH protected resources

I can get the consumer key and the consumer secret from the twitter settings pa开发者_开发知识库ge for the little test app I created, but what about the other two? After a bit of googling it seems everyone thinks it's so obvious where you get this info from that it's not worth putting up, so I might be having a really dumb moment but could someone please spell it out for idiots like me please?

Edit:
OK to get these details open your app settings in Twitter and click the "My Access Token" link.
I suppose when looking for an Access Token, if you were to click on a link titled "My Access Token" might help. I'd love to attribute my stupidity to the wine, but really I don't know...


Almost all oauth examples on blogs seem to be examples of the authorisation phase of oauth and none focus on how to actually make requests once you have these, as once you understand how it works this part is quite obvious. Getting that initial understanding is quite difficult unfortunately.

If you're just trying access your twitter account from a script or app for yourself you can get the access token (called key in the python oauth library) and secret from dev.twitter.com at the bottom of the settings page for your app under the heading Your access token.

import oauth2 as oauth
import json

CONSUMER_KEY = "your app's consumer key"
CONSUMER_SECRET = "your app's consumer secret"
ACCESS_KEY = "your access token"
ACCESS_SECRET = "your access token secret"

consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
access_token = oauth.Token(key=ACCESS_KEY, secret=ACCESS_SECRET)
client = oauth.Client(consumer, access_token)

timeline_endpoint = "https://api.twitter.com/1.1/statuses/home_timeline.json"
response, data = client.request(timeline_endpoint)

tweets = json.loads(data)
for tweet in tweets:
    print tweet['text']

This example is using the python lib python-oauth2, which is an unfortunately named OAuth library not an OAuth2 library.

If you want to actually let other people authorise their account to be used by your app then you need to implement the redirect dance where you ask twitter for a request token/secret pair and then redirect the user to the twitter authorize page with this request token, they sign in and authorize the token and get redirected back to your application, you then exchange the request token for an access token and secret pair which you can store and use to make requests like above.

The Twitter Three-legged OAuth Example in the Readme at http://github.com/simplegeo/python-oauth2 seems to cover what needs to be done


Personally I use tweepy, it provides a nice python wrapper to Twitter's API

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜