gpt4 book ai didi

python - 属性错误: 'OAuthRemoteApp' object has no attribute 'authorized_response'

转载 作者:太空宇宙 更新时间:2023-11-03 17:23:26 34 4
gpt4 key购买 nike

我正在尝试使用 Twitter 登录身份验证来授权我的应用程序,但登录 Twitter 后,它没有重定向到我的主页。显示错误:

<强> SCREENSHOT

这是我的源代码:

from flask import Flask
from flask import g, session, request, url_for, flash
from flask import redirect, render_template
from flask_oauth import OAuth


app = Flask(__name__)
app.debug = True
app.secret_key = 'development'

oauth = OAuth()

# Use Twitter as example remote application
twitter = oauth.remote_app('twitter',
base_url='https://api.twitter.com/1/',
request_token_url='https://api.twitter.com/oauth/request_token',
access_token_url='https://api.twitter.com/oauth/access_token',
authorize_url='https://api.twitter.com/oauth/authorize',
consumer_key='xxxxxxx',
consumer_secret='xxxxxxx'
)


@twitter.tokengetter
def get_twitter_token():
if 'twitter_oauth' in session:
resp = session['twitter_oauth']
return resp['oauth_token'], resp['oauth_token_secret']


@app.before_request
def before_request():
g.user = None
if 'twitter_oauth' in session:
g.user = session['twitter_oauth']


@app.route('/')
def index():
tweets = None
if g.user is not None:
resp = twitter.request('statuses/home_timeline.json')
if resp.status == 200:
tweets = resp.data
else:
flash('Unable to load tweets from Twitter.')
return render_template('index.html', tweets=tweets)


@app.route('/tweet', methods=['POST'])
def tweet():
if g.user is None:
return redirect(url_for('login', next=request.url))
status = request.form['tweet']
if not status:
return redirect(url_for('index'))
resp = twitter.post('statuses/update.json', data={
'status': status
})
if resp.status == 403:
flash('Your tweet was too long.')
elif resp.status == 401:
flash('Authorization error with Twitter.')
else:
flash('Successfully tweeted your tweet (ID: #%s)' % resp.data['id'])
return redirect(url_for('index'))


@app.route('/login')
def login():
callback_url = url_for('oauthorized', next=request.args.get('next'))
return twitter.authorize(callback=callback_url or request.referrer or None)


@app.route('/logout')
def logout():
session.pop('twitter_oauth', None)
return redirect(url_for('index'))


@app.route('/oauthorized')
def oauthorized():

resp = twitter.authorized_response()

if resp is None:
flash('You denied the request to sign in.')
else:
session['twitter_oauth'] = resp
return redirect(url_for('index'))


if __name__ == '__main__':
app.run()

请帮助我...任何形式的帮助将不胜感激!

最佳答案

在您的 oauthorized 函数中删除 resp = twitter.authorized_response() 语句,并向该函数添加一个 resp 参数。它会是这样的:

@app.route('/oauthorized')
@twitter.authorized_response
def oauthorized(resp):
if resp is None:
flash('You denied the request to sign in.')
else:
session['twitter_oauth'] = resp
return redirect(url_for('index'))

关于python - 属性错误: 'OAuthRemoteApp' object has no attribute 'authorized_response' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32880884/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com