开始使用 Twitter\OAuth2\Python [英] Getting started with Twitter\OAuth2\Python

查看:67
本文介绍了开始使用 Twitter\OAuth2\Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 python 连接到 twitter,但我发现它真的很令人沮丧.
我读到的一切都表明我需要一个消费者密钥、一个消费者秘密、一个访问密钥和一个访问秘密——例如:使用python OAUTH2访问OAUTH保护资源

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

我可以从我创建的小测试应用程序的 twitter 设置页面获取消费者密钥和消费者秘密,但另外两个呢?经过一番谷歌搜索后,似乎每个人都认为从哪里获得这些信息非常明显,不值得发布,所以我可能有一个非常愚蠢的时刻,但有人可以为像我这样的白痴拼出来吗?

I can get the consumer key and the consumer secret from the twitter settings page 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?


要获取这些详细信息,请在 Twitter 中打开您的应用设置,然后单击 我的访问令牌" 链接.
我想在寻找访问令牌时,如果您单击标题为我的访问令牌"的链接可能会有所帮助.我很想把我的愚蠢归因于酒,但我真的不知道......


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...

推荐答案

几乎所有博客上的 oauth 示例似乎都是 oauth 授权阶段的示例,并且没有一个关注一旦你拥有这些,就像一旦你了解它是如何工作的这部分是很明显的.不幸的是,获得最初的理解是相当困难的.

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.

如果您只是尝试通过脚本或应用程序访问您的 Twitter 帐户,您可以从 dev.twitter.com 位于您应用的设置页面底部 您的访问令牌 标题下.

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']

此示例使用的是 python 库 python-oauth2,这是一个不幸命名的 OAuth 库不是 OAuth2 库.

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

如果你真的想让其他人授权他们的帐户被你的应用程序使用,那么你需要实现重定向舞蹈,你向 twitter 请求请求令牌/秘密对,然后将用户重定向到 twitter 授权页面此请求令牌,他们登录并授权令牌并重定向回您的应用程序,然后您将请求令牌交换为访问令牌和秘密对,您可以存储并使用它们来发出上述请求.

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.

http://github.com/simplegeo/python 自述文件中的 Twitter 三足 OAuth 示例-oauth2 似乎涵盖了需要做的事情

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

这篇关于开始使用 Twitter\OAuth2\Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆