Twitter Oauth 回调 URI / 重定向 URL。 Python Flask Oauthlib

问题描述 投票:0回答:0

我正在尝试在我的应用程序中添加 Twitter 登录。我正在使用 python flask 和 oauthlib。 我得到这个错误:

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."}]}'.

据我所知,问题与 Twitter 开发者平台中的 Oauth 回调 URI / 重定向 URL 有关。

twitter app settings image

我的 oauthlib 配置:

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

代码:

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

我一直在浏览stackoverflow

python flask twitter oauth flask-oauthlib
© www.soinside.com 2019 - 2024. All rights reserved.