Kirill Stepankov
Kirill Stepankov

Reputation: 99

Twitter Oauth Callback URI / Redirect URL. Python Flask Oauthlib

I am trying to add twitter login in my app. I'M using python flask and oauthlib. And I get this error: authlib.integrations.base_client.errors.OAuthError: fetch_token_denied: Token request failed with code 401, response was '{"errors":[{"code":32,"message":"Could not authenticate you."}]}'.

As far as I understand the problem is related to Oauth Callback URI / Redirect URL in Twitter Developer Platform.

twitter app settings image

My oauthlib Config:

GOOGLE_CLIENT_ID = os.getenv('GOOGLE_CLIENT_ID')
GOOGLE_CLIENT_SECRET = os.getenv('GOOGLE_CLIENT_SECRET')
oauth = OAuth(app)
oauth.register(
    name='twitter',
    api_base_url='https://api.twitter.com/1.1/',
    request_token_url='https://api.twitter.com/oauth/request_token',
    access_token_url='https://api.twitter.com/oauth/access_token',
    authorize_url='https://api.twitter.com/oauth/authenticate',
)

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)
    return oauth.twitter.authorize_redirect(redirect_uri)

@bp.route('/aauthorize')
def authorize():
    token = oauth.twitter.authorize_access_token()
    resp = oauth.twitter.get('account/verify_credentials.json')
    user = resp.json()
    print(token)
    print(user)
    #resp.raise_for_status()
    #profile = resp.json()
    # do something with the token and profile
    return redirect(url_for('main.index'))

I have been browsing stackoverflow

Upvotes: 1

Views: 387

Answers (0)

Related Questions