使用Twython发送推文,twitter api

2024-04-26 08:08:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图让python使用Twython为我发送一条tweet,但是由于某些原因,我所尝试的一切都不起作用。

我遵循了Twython README但仍然无法实现我想要的。

下面是我最近尝试的代码:

from twython import Twython, TwythonError

APP_KEY = "KEYHERE"
APP_SECRET = "SECRETHERE"

twitter = Twython(APP_KEY, APP_SECRET)
auth = twitter.get_authentication_tokens()

OAUTH_TOKEN = auth['oauth_token']
OAUTH_TOKEN_SECRET = auth['oauth_token_secret']

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

try:
    twitter.update_status(status='See how easy this was?')
except TwythonError as e:
    print e

在运行上述代码时,我得到以下回溯错误:

Twitter API returned a 401 (Unauthorized), Invalid or expired token

有人知道我做错了什么吗?更重要的是,我该如何解决这个问题?

我没有足够的积分来支付赏金,但我真的很感谢你的帮助!

提前谢谢

编辑

Traceback (most recent call last):
  File "C:\testtweet.py", line 20, in <module>
    final_step = twitter.get_authorized_tokens(oauth_verifier)
  File "C:\Python27\lib\site-packages\twython\api.py", line 313, in get_authorized_tokens
    raise TwythonError('Unable to decode authorized tokens.')
TwythonError: Unable to decode authorized tokens.

上面是@justhalf提供的代码的回溯

谢天谢地


Tags: key代码tokenauthappgetsecrettwitter
3条回答

代码返回Twitter API returned a 401 (Unauthorized), Invalid or expired token的原因是,在调用get_authentication_tokens()之后,它需要oauth_verifier 尝试以下步骤以成功调用twitter&;获取oauth_verifier

from twython import Twython, TwythonError

twitter = Twython(APP_KEY, APP_SECRET)

auth = twitter.get_authorization_tokens(callback_url='http://google.com')
oauth_token = auth['oauth_token']
oauth_token_secret = auth['oauth_token_secret']
print auth
print auth['auth_url']

auth['auth_url']将打印如下响应:

https://api.twitter.com/oauth/authenticate?oauth_token=xxxxxxxxx

浏览到此URL以授权您的应用

enter image description here

在应用程序获得授权后,它将把客户端发送到您的callback_url

callback_url将附加oauth_verifier

类似于http://google.com/?oauth_verifier=xxxxxx&oauth_token=xxxxxx

根据您正在使用的webframework,您需要获取oauth_verifier的响应 现在创建一个新的Twython实例

twitter = Twython(APP_KEY, APP_SECRET, oauth_token, oauth_token_secret)
final_tokens = twitter.get_authorized_tokens(oauth_verifier)

print final_tokens

# these are the keys you will use to make calls on the users behalf from here on forward
f_oauth_token = final_tokens['oauth_token']
f_oauth_token_secret = final_tokens['oauth_token_secret']

更新Twitter状态:

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN=f_oauth_token, OAUTH_TOKEN_SECRET=f_oauth_token_secret)

try:
    twitter.update_status(status='See how easy this was?')
except TwythonError as e:
    print e

如@Games Brainiac所述,更新twitter的应用程序设置

让我们一起找出哪里出了问题。

我在documentation中注意到:

Now that you have the oauth_verifier stored to a variable, you'll want to create a new instance of Twython and grab the final user tokens

下面的代码:

twitter = Twython(APP_KEY, APP_SECRET,
              OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

final_step = twitter.get_authorized_tokens(oauth_verifier)

看来你错过了final_step

之后(从文档中):

Once you have the final user tokens, store them in a database for later use!:

OAUTH_TOKEN = final_step['oauth_token']
OAUTH_TOKEN_SECERT = final_step['oauth_token_secret']

在那之后,我想您需要创建另一个新的Twython实例,其中包含最后的OAUTH_TOKENOAUTH_TOKEN_SECRET。所以完整的代码应该是这样的,我想:

from twython import Twython, TwythonError
import requests

APP_KEY = "KEYHERE"
APP_SECRET = "SECRETHERE"

twitter = Twython(APP_KEY, APP_SECRET)
auth = twitter.get_authentication_tokens()

OAUTH_TOKEN = auth['oauth_token']
OAUTH_TOKEN_SECRET = auth['oauth_token_secret']

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

### This is the part you're missing ###
oauth_verifier_url = auth['auth_url']
oauth_verifier = requests.get(oauth_verifier_url)

# Getting the FINAL authentication tokens
final_step = twitter.get_authorized_tokens(oauth_verifier)

OAUTH_TOKEN = final_step['oauth_token']
OAUTH_TOKEN_SECRET = auth['oauth_token_secret']

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
### Up until this line ###

try:
    twitter.update_status(status='See how easy this was?')
except TwythonError as e:
    print e

我没有Twitter应用程序密钥,所以无法尝试。

但我想这足以解决你的问题。我希望这能有帮助。=)

注意,我使用了Python中的^{}包。

有一种更简单的方式来更新你的帖子,而不是Twython实际显示给你的方式。不过,这需要在API控制台空间上做更多的工作,所以让我开始吧。

  1. 首先,你需要去你的apps page。选择了当前正在使用的应用程序后,请查看给定的页面。默认情况下,您应该位于详细信息选项卡。 enter image description here

  2. 既然您已经到达了应该到达的位置,请单击设置选项卡上的详细信息选项卡旁边,如上图所示。

  3. 之后,向下滚动,直到看到以下内容: enter image description here

  4. 单击上面显示的选项。现在,在您选择了选项后,向下滚动,直到您看到一个蓝色按钮,上面写着“更新此twitter的应用程序设置”。

  5. 现在,回到您的详细信息选项卡。转到底部并生成所需的令牌,请注意,可能需要单击两次按钮才能使其正常工作(同时,请确保生成令牌时,您的访问级别为读、写和直接消息): enter image description here

  6. 现在你已经拥有了连接和发布到你的twitter帐户所需的一切。你有Consumer keyConsumer Secret,还有Access tokenAccess token secret。你有你需要的一切。

好的,现在去你的代码编辑器,写下下面的锅炉板代码(这些键不起作用,我只是去掉了应用程序,所以这里没有黑客攻击:p我给它们只是为了表示你应该期望的键的长度):

from twython import Twython

APP_KEY = ''  # Customer Key here
APP_SECRET = ''  # Customer secret here
OAUTH_TOKEN = '1936951807-z5bBNING8P1TU2onWvJh5dh8hoYlYAmNOaAx2OX'  # Access Token here
OAUTH_TOKEN_SECRET = 'QWJEZ7ridSeZGdxJELSBk7mupCpMA9q9sLCou5ywg'  # Access Token Secret here

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

twitter.update_status(status="Hello from Python! :D")

在这之后,查看你的twitter,你会看到一条新的tweet,上面写着“来自Python的Hello!:D英寸。

相关问题 更多 >