Tweepy和SocialRegistration问题
我已经成功安装了Tweepy,也成功安装了SocialRegistration(Twitter的认证工具)。但是现在我想在Tweepy中做一些需要认证的事情,却总是出现“需要认证”的错误页面。这该怎么解决呢?我需要把SocialRegistration和Tweepy“连接”起来吗?是不是需要把一些令牌传给Tweepy?有没有用过这两个模块的人能帮帮我?提前谢谢大家。
2 个回答
0
我一直在使用以下代码来通过Tweepy和django-socialregistration访问Twitter的API:
OAUTH_TOKEN_SECRET = request.session['oauth_api.twitter.com_access_token']['oauth_token_secret']
OAUTH_TOKEN = request.session['oauth_api.twitter.com_access_token']['oauth_token']
CONSUMER_KEY = settings.TWITTER_CONSUMER_KEY
CONSUMER_SECRET = settings.TWITTER_CONSUMER_SECRET_KEY
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
api = tweepy.API(auth)
# Get user's info
user_info = api.me()
1
你想用这两个模块(social-registration和tweepy)实现什么,其实不是很清楚。
我猜测你是想用tweepy的API做一些需要认证的事情(比如更新状态),所以这里有一个简单的oauth认证的方法,适用于tweepy。
#Requirements:
#tweepy
#consumer_key
#consumer_secret
#access_token_key
#access_token_secret
import tweepy
#build auth handler
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token_key, access_token_secret)
# initialize tweepy API
api = tweepy.API(auth)
# thats it. you can start doing stuff that requires authentication =)
api.update_status('tweepy + oauth!')
总之,希望这能帮到你,不管你想实现什么。如果需要的话,tweepy的文档写得很好,你可以去看看:http://joshthecoder.github.com/tweepy/docs/index.html(相信我,读起来很简单)
祝好运