【问题标题】:Tweepy and flask: failed to get request tokenTweepy 和烧瓶:未能获取请求令牌
【发布时间】:2012-11-06 22:32:55
【问题描述】:

我尝试使用 Tweepy 和 Flask,但我似乎得到了请求令牌。

这是失败的代码。

CONSUMER_TOKEN='fill this out'
CONSUMER_SECRET='and this'
CALLBACK_URL = 'http://localhost:5000/verify'
session = dict()
db = dict() #you can save these values to a database

@app.route("/")
def send_token():
    auth = tweepy.OAuthHandler(CONSUMER_TOKEN, 
        CONSUMER_SECRET, 
        CALLBACK_URL)

    try: 
        #get the request tokens
        redirect_url= auth.get_authorization_url()
        session['request_token']= (auth.request_token.key,
            auth.request_token.secret)
    except tweepy.TweepError:
        print 'Error! Failed to get request token'

    #this is twitter's url for authentication
    return flask.redirect(redirect_url) 

来自this github 项目。当然,我替换了消费者令牌/秘密。

为什么 auth.get_authorization_url() 会失败?

谢谢。

完整的日志是这样的

 * Running on http://127.0.0.1:5000/
Error! Failed to get request token
127.0.0.1 - - [20/Nov/2012 22:54:28] "GET / HTTP/1.1" 500 -
127.0.0.1 - - [20/Nov/2012 22:54:29] "GET /favicon.ico HTTP/1.1" 404 -

【问题讨论】:

  • 您能否分享有关您收到的错误的更多详细信息?
  • 我只是得到'错误!无法在控制台中获取请求令牌'和“内部服务器错误服务器遇到内部错误,无法完成您的请求。服务器过载或应用程序出错。”在浏览器中。然后脚本似乎正在 localhost:5000 加载 Flask 服务器(根据终端)。
  • 记录原始异常消息可能会有所帮助

标签: python flask tweepy


【解决方案1】:

看起来 Tweepy 默认为 oauth 端点使用 http:// 协议。您需要更改代码以强制它使用https://。这可能会解决您的问题。

当你像这样实例化 OAuthHandler 类时你会这样做:

auth = tweepy.OAuthHandler(CONSUMER_TOKEN, 
    CONSUMER_SECRET, 
    CALLBACK_URL, secure=True)

【讨论】:

  • 谢谢,但没有帮助。我仍然收到相同的“错误!无法获取请求令牌”。
猜你喜欢
  • 2021-12-30
  • 2014-09-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-23
  • 1970-01-01
  • 2017-08-24
  • 2013-06-13
  • 2013-08-13
相关资源
最近更新 更多