我试图让python使用特沃顿为我发送一条推特,但出于某种原因,我所尝试的一切都不起作用。
我已经跟踪了Twython自述,但仍然无法获得我想要的。
下面是我最新的尝试代码:
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底提供的代码中收到的回溯信息。
谢谢SMNALLY
发布于 2013-10-05 10:15:43
与Twython实际显示的方式相比,有一种更新帖子的方法要简单得多。不过,在API控制台空间上需要做更多的工作,所以让我开始。



Consumer key和Consumer Secret,Access token和Access 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,您应该会看到一条新的twitter,上面写着“Python!:d你好”。
发布于 2013-10-03 01:38:16
让我们一起找出到底出了什么问题。
我在文档中注意到了这一点
现在已经将
oauth_verifier存储到一个变量中,您将需要创建Twython的一个新实例并获取最终的用户令牌
下面的代码是:
twitter = Twython(APP_KEY, APP_SECRET,
OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
final_step = twitter.get_authorized_tokens(oauth_verifier)看来你错过了final_step
然后(从文档中):
拥有最终用户令牌后,将其存储在数据库中供以后使用!:
OAUTH_TOKEN = final_step['oauth_token']
OAUTH_TOKEN_SECERT = final_step['oauth_token_secret']在此之后,我想您将需要使用最后的OAUTH_TOKEN和OAUTH_TOKEN_SECRET创建另一个Twython实例。所以完整的代码应该是这样的,我想:
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我没有推特应用键,所以我无法尝试。
但我想这应该足以解决你的问题了。我希望这能帮到你。=)
请注意,我使用了来自Python的requests包。
发布于 2013-10-09 09:37:08
代码返回oauth_verifier的原因是在调用get_authentication_tokens()之后需要get_authentication_tokens(),尝试使用这些步骤成功地调用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'将打印一个响应,类似于:
浏览此URL以授权您的应用程序

应用程序获得授权后,它将将客户端发送到您的callback_url
callback_url将被附加到oauth_verifier
有点像http://google.com/?oauth_verifier=xxxxxx&oauth_token=xxxxxx
根据您正在使用的new框架,您需要获取,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更新twitter的应用程序设置,正如@Games Brainiac所提到的
https://stackoverflow.com/questions/19095257
复制相似问题