Tweepy examples that work as of 2023/2/25 Create_Friendship

import tweepy
import logging
import os
from dotenv import load_dotenv

# Loads the .env file for the credentials
load_dotenv()

# Credentials set in the .env file
consumer_key = os.environ.get('consumer_key')
print('consumer_key:',consumer_key)
consumer_secret = os.environ.get('consumer_secret')
print('consumer_secret:',consumer_secret)
access_token = os.environ.get('access_token')
print('access_token:',access_token)
access_token_secret = os.environ.get('access_token_secret')
print('access_token_secret:',access_token_secret)

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)


# auth = tweepy.OAuth2AppHandler(consumer_key, consumer_secret)

api = tweepy.API(auth)

output = open('stream_output.txt', 'w')


friends = []
screen_name = 'lansa'
print(f"Friends (accounts {screen_name} follows)")
for page in tweepy.Cursor(api.get_friends, screen_name=screen_name,
                          count=20).pages(1):
    for user in page:
        name = f"{user.id} - {user.name} (@{user.screen_name})"
        print(name)
        friends.append(name)
        api.create_friendship(user_id=user.id)
    print(len(page))
 
print(f"Friends: {len(friends)}")

Be sure to create your environment variables first for keys and tokens. Follows first 20 followers of lansa