AutoGPT: Send_tweet gives an error

⚠️ Search for existing issues first ⚠️

  • I have searched the existing issues, and there is no existing issue for my problem

GPT-3 or GPT-4

  • I am using Auto-GPT with GPT-3 (GPT-3.5)

Steps to reproduce 🕹

I get the following error while the send_tweet command using the stable version. The API keys should be correct.

NEXT ACTION: COMMAND = send_tweet ARGUMENTS = {‘text’: ‘Test’} SYSTEM: Command send_tweet returned: Error: ‘Forbidden’ object has no attribute ‘reason’

Current behavior 😯

No response

Expected behavior 🤔

No response

Your prompt 📝

# Paste your prompt here

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 28 (2 by maintainers)

Most upvoted comments

I’ve figured it out using Bing! You need to update the code as @Kendhal7 mentioned, but V2 requires a bearer token and will need to setup OAuth 2.0 in your dev portal. Here’s the solution step-by-step (for free twitter API)

  1. Update the twitter.py file to use the following code for API v2 and include the bearer token in the request :
import os

import tweepy
from dotenv import load_dotenv

load_dotenv()

def send_tweet(tweet_text):
    consumer_key = os.environ.get("TW_CONSUMER_KEY")
    consumer_secret = os.environ.get("TW_CONSUMER_SECRET")
    access_token = os.environ.get("TW_ACCESS_TOKEN")
    access_token_secret = os.environ.get("TW_ACCESS_TOKEN_SECRET")
    bearer_token = os.environ.get("TW_BEARER_TOKEN")
    # Authenticate to Twitter
    client = tweepy.Client(bearer_token=bearer_token,
                    consumer_key=consumer_key,
                    consumer_secret=consumer_secret,
                    access_token=access_token,
                    access_token_secret=access_token_secret)
    # Send tweet
    try:
        client.create_tweet(text=tweet_text)
        print("Tweet sent successfully!")
    except tweepy.TweepyException as e:
        print("Error sending tweet: {}".format(e.reason))

  1. Go to your Twitter Developer Portal (https://developer.twitter.com/en).
  2. Select your project and app.
  3. In the settings tab scroll down and hit the edit/set up button of User authentication settings.
  4. Change App permissions to Read and write and Direct message.
  5. Set to web app (2nd option).
  6. Set Callback URI & Website just to https://twitter.com/ since it doesn’t matter.
  7. Save and regenerate your Keys and Tokens in the Keys and Tokens Tab. (ignore the newly created client ID/secret generated after setting up auth, those are not needed)
  8. Generate a bearer token.
  9. Update the .env file with new tokens and add the bearer token like so:
TW_CONSUMER_KEY=[YOUR CREDENTIALS]
TW_CONSUMER_SECRET=[YOUR CREDENTIALS]
TW_ACCESS_TOKEN=[YOUR CREDENTIALS]
TW_ACCESS_TOKEN_SECRET=[YOUR CREDENTIALS]
TW_BEARER_TOKEN=[YOUR CREDENTIALS]

That should work 😃

Update: See @mossy426 solution below.

The problem is: for Free Twitter API account, it can only use Twitter API v2 for tweet creation. https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api

You can use tweepy.Client instead of tweepy.API, which supports Twitter API v2.

However, after I change to tweepy.Client, the following error occurs:

{
    "title": "Forbidden",
    "status": 403,
    "detail": "Your client app is not configured with the appropriate oauth1 app permissions for this endpoint.",
    "type": "https://api.twitter.com/2/problems/oauth1-permissions"
}

I am not able to set the write permission for Access Token and Secret in Developer Portal. The official docs tell me only paid account can set permissions. https://developer.twitter.com/en/docs/apps/app-permissions https://stackoverflow.com/a/74495869

@mossy426 @Kendhal7 Thanks for your help! Can I make a pull request to wrap all the code changes and update readme document?

Try modifying the twitter.py file in /autogpt/commands/ folder with this:


import os

import tweepy
from dotenv import load_dotenv

load_dotenv()


def send_tweet(tweet_text):
    consumer_key = os.environ.get("TW_CONSUMER_KEY")
    consumer_secret = os.environ.get("TW_CONSUMER_SECRET")
    access_token = os.environ.get("TW_ACCESS_TOKEN")
    access_token_secret = os.environ.get("TW_ACCESS_TOKEN_SECRET")
    # Authenticate to Twitter
    client = tweepy.Client(consumer_key=consumer_key,
                    consumer_secret=consumer_secret,
                    access_token=access_token,
                    access_token_secret=access_token_secret)
    # Send tweet
    try:
        client.create_tweet(text=tweet_text)
        print("Tweet sent successfully!")
    except tweepy.TweepyException as e:
        print("Error sending tweet: {}".format(e.reason))

I’ve figured it out using Bing! You need to update the code as @Kendhal7 mentioned, but V2 requires a bearer token and will need to setup OAuth 2.0 in your dev portal. Here’s the solution step-by-step (for free twitter API)

  1. Update the twitter.py file to use the following code for API v2 and include the bearer token in the request :
import os

import tweepy
from dotenv import load_dotenv

load_dotenv()

def send_tweet(tweet_text):
    consumer_key = os.environ.get("TW_CONSUMER_KEY")
    consumer_secret = os.environ.get("TW_CONSUMER_SECRET")
    access_token = os.environ.get("TW_ACCESS_TOKEN")
    access_token_secret = os.environ.get("TW_ACCESS_TOKEN_SECRET")
    bearer_token = os.environ.get("TW_BEARER_TOKEN")
    # Authenticate to Twitter
    client = tweepy.Client(bearer_token=bearer_token,
                    consumer_key=consumer_key,
                    consumer_secret=consumer_secret,
                    access_token=access_token,
                    access_token_secret=access_token_secret)
    # Send tweet
    try:
        client.create_tweet(text=tweet_text)
        print("Tweet sent successfully!")
    except tweepy.TweepyException as e:
        print("Error sending tweet: {}".format(e.reason))
  1. Go to your Twitter Developer Portal (https://developer.twitter.com/en).
  2. Select your project and app.
  3. In the settings tab scroll down and hit the edit/set up button of User authentication settings.
  4. Change App permissions to Read and write and Direct message.
  5. Set to web app (2nd option).
  6. Set Callback URI & Website just to https://twitter.com/ since it doesn’t matter.
  7. Save and regenerate your Keys and Tokens in the Keys and Tokens Tab. (ignore the newly created client ID/secret generated after setting up auth, those are not needed)
  8. Generate a bearer token.
  9. Update the .env file with new tokens and add the bearer token like so:
TW_CONSUMER_KEY=[YOUR CREDENTIALS]
TW_CONSUMER_SECRET=[YOUR CREDENTIALS]
TW_ACCESS_TOKEN=[YOUR CREDENTIALS]
TW_ACCESS_TOKEN_SECRET=[YOUR CREDENTIALS]
TW_BEARER_TOKEN=[YOUR CREDENTIALS]

That should work 😃

What’s in the callback URI?