【问题标题】:Error 901: Invalid redirect_uri: Given URL is not permitted by the Application configuration错误 901:无效的 redirect_uri:应用程序配置不允许给定 URL
【发布时间】:2015-11-06 23:03:25
【问题描述】:

我正在尝试使用 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 上读到,我应该将 Facebook 应用程序设置中的站点 URL 设置为我的 URL,即 http://localhost:5000/,我拥有该 URL,但仍然收到错误消息。我从Flask Mega Tutorial 得到了代码,所以我不认为有任何错误。

是什么导致了问题以及如何解决?谢谢。

【问题讨论】:

  • 您在 Facebook 中为该应用设置了哪些设置,以及您在本地访问该应用所使用的 URL 到底是什么?
  • 我正在使用127.0.0.1:5000 在本地访问应用程序。除了我在问题中所述的网站 URL 之外,我没有更改应用程序中的任何设置

标签: python facebook oauth flask rauth


【解决方案1】:

如果您在 Facebook 中的网站 URL 是 http://localhost:5000,您需要在浏览器中通过 http://localhost:5000 访问应用程序 - 在这种情况下,Facebook 无法识别 http://127.0.0.1:5000http://localhost:5000 相同,即为什么您会收到无效的 URL 消息。

【讨论】:

    猜你喜欢
    • 2013-05-02
    • 1970-01-01
    • 2011-04-12
    • 2013-10-06
    • 2013-01-06
    • 2016-01-06
    相关资源
    最近更新 更多