小编Dwi*_*ght的帖子

如何使用 Twitter API v2 的 tweepy 通过用户名获取用户 ID?

我正在尝试从 geeksforgeeks 复制此代码片段,只不过它使用 Twitter API v1.1 的 oauth,而我使用 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.
Run Code Online (Sandbox Code Playgroud)

这是我的:

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, …
Run Code Online (Sandbox Code Playgroud)

python twitter tweepy twitterapi-python

6
推荐指数
2
解决办法
9250
查看次数

标签 统计

python ×1

tweepy ×1

twitter ×1

twitterapi-python ×1