pythonfacebookoauthflaskrauth

Error 901: Invalid redirect_uri: Given URL is not permitted by the Application configuration


I'm trying to set up a Flask login system using Rauth however I receive the error:

{
   "error": {
      "message": "Invalid redirect_uri: Given URL is not permitted by the Application configuration",
      "type": "OAuthException",
      "code": 191
   }
}

Though I'm not sure if this is an issue with the code or an issue with my Facebook app setup here is my code:

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())
        )

And here are my views:

@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'))

I read on some 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 so I don't believe there are any errors with that.

What is causing the issue and how can it be solved? Thanks.


Solution

  • If your site URL in Facebook is 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 in this situation, which is why you get the invalid URL message.