0

我正在尝试使用 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'))

我阅读了其他一些答案,我应该将 Facebook 应用程序设置中的站点 URL 设置为我的 URL,即http://localhost:5000/,我拥有该 URL,但仍然收到错误。我从Flask Mega Tutorial获得了代码,所以我不相信有任何错误。

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

4

1 回答 1

0

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

于 2015-08-14T01:29:26.350 回答