pythontwittertweepy

Working with Twitter OAuth 2.0 Authorization Code Flow with PKCE (User Context)


I'm trying to play with the twitter API using tweepy.

This is my code:

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)

On the last line I'm getting this error:

Authenticating with OAuth 2.0 Application-Only is forbidden for this endpoint. Supported authentication types are [OAuth 1.0a User Context, OAuth 2.0 User Context]

So, it's telling me that I'm using OAuth 2.0 Application-Only, but everything I read says that this is how you get a user access token using OAuth 2.0 User Context.

This answer helped me and got me to this point, but I can't pass that error.

What am I missing?


Solution

  • the problem was in my code in this line:

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

    it should be:

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