Tweepy Bot with Google Cloud Functions and Cloud Scheduler no longer working with Twitter API

43 Views Asked by At

Using my twitter bot with Tweepy is no longer working. I have access to the free version with limited V1.1 access only and V2 access. I've hit a wall and am unsure what the problem is with this bot that was working fine a few days ago.

This is what my code looks like:

import tweepy
import random
import os

consumer_key = os.environ["TWITTER_CONSUMER_KEY"] = ""
consumer_secret = os.environ["TWITTER_CONSUMER_SECRET"] = ""
access_token = os.environ["TWITTER_ACCESS_TOKEN"] = ""
access_token_secret = os.environ["TWITTER_ACCESS_TOKEN_SECRET"] = ""
bearer_token=None
client = tweepy.Client(bearer_token,consumer_key,consumer_secret,access_token,access_token_secret)
client.get_me()

def post_tweet(request, context):


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

    tweets = [
    "TO BE FILLED",
    "TO BE FILLED",
    "TO BE FILLED",
    "TO BE FILLED"
    ]
    random_tweet = random.choice(tweets)
    api.update_status(random_tweet)

    return "Tweet posted!"

This is the error I am receiving:

tweepy.errors.Forbidden: 403 Forbidden

at .request ( /layers/google.python.pip/pip/lib/python3.9/site-packages/tweepy/api.py:271 ) at .update_status ( /layers/google.python.pip/pip/lib/python3.9/site-packages/tweepy/api.py:979 ) at .wrapper ( /layers/google.python.pip/pip/lib/python3.9/site-packages/tweepy/api.py:46 ) at .post_tweet ( /workspace/main.py:38 ) at .view_func ( /layers/google.python.pip/pip/lib/python3.9/site-packages/functions_framework/init.py:222 ) at .dispatch_request ( /layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py:1502 ) at .full_dispatch_request ( /layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py:1516 ) at .full_dispatch_request ( /layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py:1518 ) at .wsgi_app ( /layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py:2073 )

I tried tweaking the code, adding tweepy.Client, but I am still getting the same error.

0

There are 0 best solutions below