I’m trying to replicate this snippet from geeksforgeeks, except that it uses the oauth for Twitter API v1.1 while I the API v2.
# the screen name of the user
screen_name = "PracticeGfG"
# fetching the user
user = api.get_user(screen_name)
# fetching the ID
ID = user.id_str
print("The ID of the user is : " + ID)
OUTPUT:
The ID of the user is: 4802800777.
And here’s mine:
import os
import tweepy
API_KEY = os.getenv('API_KEY')
API_KEY_SECRET = os.getenv('API_KEY_SECRET')
BEARER_TOKEN = os.getenv('BEARER_TOKEN')
ACCESS_TOKEN = os.getenv('ACCESS_TOKEN')
ACCESS_TOKEN_SECRET = os.getenv('ACCESS_TOKEN_SECRET')
screen_name = 'nytimes'
client = tweepy.Client(consumer_key=API_KEY, consumer_secret=API_KEY_SECRET, access_token=ACCESS_TOKEN, access_token_secret=ACCESS_TOKEN_SECRET)
user = client.get_user(screen_name)
id = user.id_str
print(id)
When I run mine, it returns this error:
TypeError: get_user() takes 1 positional argument but 2 were given.
Can you give me hints in what did I miss? Thanks ahead.
2
Answers
get_user
have the following signature.Notice the
*
.*
is used to force the caller to use named arguments. For example, This won’t work.But this will
So to answer your question you should call the
get_user
method like this.I believe that client.get_user(username=screen_name) returns a class so this function works for me: