I use authlib to authenticate with github. In developer settings github I set Authorization callback URL http://127.0.0.1:5000/complete
But everything works very, very strangely, sometimes the authentication works, but more often I get the error:
authlib.integrations.base_client.errors.OAuthError: redirect_uri_mismatch: The redirect_uri MUST match the registered callback URL for this application.
Sometimes I can get an error, and then click on the authentication again and everything will work.
My code:
from app.authentication.oauth import bp
from flask import url_for, render_template, redirect
from app import oauth
@bp.route('/alogin')
def login():
redirect_uri = url_for('oauth.authorize', _external=True)
print(redirect_uri)
return oauth.github.authorize_redirect(redirect_uri)
@bp.route('/complete')
def authorize():
token = oauth.github.authorize_access_token()
resp = oauth.github.get('user', token=token)
resp.raise_for_status()
user = resp.json()
print(token)
print(user)
#profile = resp.json()
# do something with the token and profile
return redirect(url_for('main.index'))
In my opinion, the uri matches, but it works very strangely
this question is very similar to mine
But it can't help with my problem.
Ok, I understood what the problem was. I changed 127.0.0.1 to localhost in GitHub app settings. And it works.