我尝试使用Rauth设置Flask登录系统但是我收到错误:
class OAuthSignIn(object):
providers = None
def __init__(self, provider_name):
self.provider_name = provider_name
credentials = app.config['OAUTH_CREDENTIALS'][provider_name]
self.consumer_id = credentials['id']
self.consumer_secret = credentials['secret']
def authorize(self):
pass
def callback(self):
pass
def get_callback_url(self):
return url_for('account.oauth_callback', provider=self.provider_name, _external=True)
@classmethod
def get_provider(self, provider_name):
if self.providers is None:
self.providers = {}
for provider_class in self.__subclasses__():
provider = provider_class()
self.providers[provider.provider_name] = provider
return self.providers[provider_name]
class FacebookSignIn(OAuthSignIn):
def __init__(self):
super(FacebookSignIn, self).__init__('facebook')
self.service = OAuth2Service(
name='facebook',
client_id=self.consumer_id,
client_secret=self.consumer_secret,
authorize_url='https://graph.facebook.com/oauth/authorize',
access_token_url='https://graph.facebook.com/oauth/access_token',
base_url='https://graph.facebook.com/'
)
def authorize(self):
return redirect(self.service.get_authorize_url(
scope='email',
response_type='code',
redirect_uri=self.get_callback_url())
)
虽然我不确定这是代码的问题,但我的Facebook应用设置存在问题,这是我的代码:
@account.route('/authorize/<provider>')
def oauth_authorize(provider):
if checkLoggedIn():
return redirect(url_for('main.index'))
oauth = OAuthSignIn.get_provider(provider)
return oauth.authorize()
@account.route('/callback/<provider>')
def oauth_callback(provider):
if checkLoggedIn():
return redirect(url_for('index'))
oauth = OAuthSignIn.get_provider(provider)
socialID, name, email = oauth.callback()
print socialID, name, email
if socialID is None:
flash('Authentication failed.')
return redirect(url_for('index'))
user = User.query.filter_by(social_id=social_id).first()
if not user:
user = User(social_id=social_id, nickname=username, email=email)
db.session.add(user)
db.session.commit()
login_user(user, True)
return redirect(url_for('index'))
以下是我的观点:
$scope.people
我在一些other answers上读到我应该将Facebook应用设置中的网站网址设置为我的网址http://localhost:5000/,我已经收到错误了。我从Flask Mega Tutorial获得了代码,所以我不相信有任何错误。
导致问题的原因是什么?如何解决?感谢。
答案 0 :(得分:0)
如果您在Facebook中的网站网址为http://localhost:5000,则需要在浏览器中通过http://localhost:5000访问该应用 - Facebook无法识别http://127.0.0.1:5000与{{3}相同在这种情况下,这就是您收到无效URL消息的原因。