Tweepy API v2 Authentication

Viewed 34

How can I test the Tweepy authentication using API v2?

My code below fails to post due to authentication however, is there any other way to test the authentication without invoking posting a Tweet (client.create_tweet) ?

import tweepy

bearer_token = "hidden"

consumer_key = "hidden"
consumer_secret = "hidden"

access_token = "hidden"
access_token_secret = "hidden"

client = tweepy.Client(bearer_token=bearer_token)

client = tweepy.Client(
    consumer_key=consumer_key,
    consumer_secret=consumer_secret,
    access_token=access_token,
    access_token_secret=access_token_secret
)

client.create_tweet(text="My first tweet")
1 Answers

You can use any of the methods listed in the documentation here.

I would suggest you to use some read method like get_user:

user = client.get_user('twitter')
print(user.name)

About your authentication itself, the problem is that you have to choose between:

  • Using the bearer token of your application to get an OAuth 2 "App-Only" authentication. You will have, basically, the same possibilies as an unregistered user on the website.

  • Using an account's tokens to get an OAuth 1.0a "User Context" authentication. You will then be able to act in the behalf of the user who possesses the tokens that you are using.

But you are trying to do both successively.

Related