如何代表已认证我的应用的用户发推文?
我有一个应用程序,用户可以通过 Twitter 注册
。我设置了权限为读取、写入和直接消息,所以当用户注册我的项目时,他就授权我可以代表他发帖。同时,在注册过程中,我还获得了他的 oauth-token 和 oauth-token secret
。
但是我不知道怎么才能代表这个用户发推文。我尝试在 https://api.twitter.com/1.1/statuses/update.json 提交一个表单,里面包含状态、我的应用密钥和秘密、用户的 oauth_token 和 secret,但我总是收到这样的回复:
{"errors":[{"message":"Bad Authentication data","code":215}]}
我使用的是 python-social-auth 来通过 Twitter 注册。
任何帮助都将不胜感激。谢谢!
1 个回答
4
你需要在请求中指定用户的 access_token
,这样Twitter才能知道当前是谁在登录。使用 python-social-auth
你可以这样做:
# given a user instance (and assuming Django ORM)
social = user.social_auth.get(provider='twitter')
backend = social.get_backend()
form_data = {
'status': 'The new twitter status goes here'
}
response = backend.get_json(
'https://api.twitter.com/1.1/statuses/update.json',
method='POST',
auth=backend.oauth_auth(social.extra_data['access_token']),
data=form_data
)