通过 PKCE(用户上下文)使用 Twitter OAuth 2.0 授权代码流程

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

我正在尝试使用 tweepy 来玩 twitter API。

这是我的代码:

oauth2_user_handler = tweepy.OAuth2UserHandler(
    client_id=client_id,
    redirect_uri=redirect_uri,
    scope=scopes,
    client_secret=client_secret,
)

auth_url = oauth2_user_handler.get_authorization_url()

print('Please authorize this application: {}'.format(auth_url))

verifier = input('Enter the authorization response URL:')
token = oauth2_user_handler.fetch_token(verifier)
client = tweepy.Client(access_token=token['access_token'])
user = client.get_me(user_auth=False)

在最后一行我收到此错误:

为此禁止使用 OAuth 2.0 仅应用程序进行身份验证 端点。支持的身份验证类型有 [OAuth 1.0a 用户 上下文,OAuth 2.0 用户上下文]

所以,它告诉我我正在使用 OAuth 2.0 Application-Only,但我读到的所有内容都表明这就是使用 OAuth 2.0 User Context 获取 user 访问令牌的方式。

这个答案帮助了我,让我走到了这一步,但我无法通过这个错误。

我错过了什么?

python twitter tweepy
1个回答
0
投票

问题出在我的这一行代码中:

client = tweepy.Client(access_token=token['access_token'])

应该是:

client = tweepy.Client(token['access_token'])
© www.soinside.com 2019 - 2024. All rights reserved.