Simple Sample for X/Twitter access with Python and https://www.tweepy.org
# Python virtual Environment python3 -m venv twitter ## Activate source twitter/bin/activate # Install tweepy pip install -r requirements.txt # run sample python tweepyTest.py
python-dotenv tweepy
# Your X API credentials consumer_key = "SECRET_CREDENTIAL" consumer_secret = "SECRET_CREDENTIAL" access_token = "SECRET_CREDENTIAL" access_token_secret = "SECRET_CREDENTIAL" bearer_token = "SECRET_CREDENTIAL"
import os
from dotenv import load_dotenv
import tweepy
# load .env-Cile
load_dotenv()
# Create a client instance for Twitter API v2
client = tweepy.Client(bearer_token=os.getenv("bearer_token"))
# Fetch a user by username
username = "IngoKaulbachQS"
user = client.get_user(username=username)
# Access the user information
print(f"User ID: {user.data.id}")
print(f"Username: {user.data.username}")
print(f"Name: {user.data.name}")
print(f"User ID: {user}")UPDATE
Updated requirement from "dotenv" to "python-dotenv".
Error messages were not helpfull, neither AI-Systems. Classic googeling and try-and-error did the job. This is no fun. Python could have done better.