错误 901:无效 redirect_uri:应用程序配置不允许给定 URL
Error 901: Invalid redirect_uri: Given URL is not permitted by the Application configuration
我正在尝试使用 Rauth 设置 Flask 登录系统,但是我收到错误:
{
"error": {
"message": "Invalid redirect_uri: Given URL is not permitted by the Application configuration",
"type": "OAuthException",
"code": 191
}
}
虽然我不确定这是代码问题还是我的 Facebook 应用程序设置问题这里是我的代码:
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())
)
这是我的观点:
@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'))
我读了一些 other answers that I should set the site URL in the Facebook app settings to my URL which is http://localhost:5000/ which I have and still receive the error. I got the code from the Flask Mega Tutorial 所以我认为这没有任何错误。
是什么导致了这个问题,如何解决?谢谢。
如果您在 Facebook 中的站点 URL 在这种情况下是 http://localhost:5000, you need to access the app via http://localhost:5000 in your browser - Facebook doesn't recognise http://127.0.0.1:5000 as being the same as http://localhost:5000,这就是您收到无效 URL 消息的原因。
我正在尝试使用 Rauth 设置 Flask 登录系统,但是我收到错误:
{
"error": {
"message": "Invalid redirect_uri: Given URL is not permitted by the Application configuration",
"type": "OAuthException",
"code": 191
}
}
虽然我不确定这是代码问题还是我的 Facebook 应用程序设置问题这里是我的代码:
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())
)
这是我的观点:
@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'))
我读了一些 other answers that I should set the site URL in the Facebook app settings to my URL which is http://localhost:5000/ which I have and still receive the error. I got the code from the Flask Mega Tutorial 所以我认为这没有任何错误。
是什么导致了这个问题,如何解决?谢谢。
如果您在 Facebook 中的站点 URL 在这种情况下是 http://localhost:5000, you need to access the app via http://localhost:5000 in your browser - Facebook doesn't recognise http://127.0.0.1:5000 as being the same as http://localhost:5000,这就是您收到无效 URL 消息的原因。