Reading Time: 3 minutes
Hello, today we are going to learn how we can automatically post a Tweet or create a Twitter bot using Python.

The first thing we are going to do is register our application on the Twitter Developer portal:
- Create an application on Twitter:
- Go to Twitter Developer and create an application. This will provide you with the necessary credentials to access the Twitter API.
Access and create a project:

Once the project is created, create an app:

Reading Time: 3 minutes
Hello, today we are going to learn how we can automatically post a Tweet or create a Twitter bot using Python.

The first thing we are going to do is register our application on the Twitter Developer portal:
- Create an application on Twitter:
- Go to Twitter Developer and create an application. This will provide you with the necessary credentials to access the Twitter API.
Access and create a project:

Once the project is created, create an app:

Click on “Overview”:

And create an app:
Add the name of the APP

And copy our keys correctly:
And we copy our keys correctly:

We also click on “Generate Access Token”:

So that we can access the APP externally, we have to configure the permissions. For this, we go to User Authentication Settings:

Click on “Set up” and configure the permissions, in this case, read and write:

We can also choose the type of APP:

And we can add the URLs it requests in case we need to configure a Webhook:

Install this library with Python:
pip install requests_oauthlib
We also need to install this one to read the .env:
pip install python-dotenv
Now, let’s go to a text editor and create this file:
import os
from dotenv import load_dotenv
import json
import oauthlib.oauth1
from requests_oauthlib import OAuth1Session
# Load environment variables from a .env file
load_dotenv()
CONSUMER_KEY = os.getenv("TWITTER_API_KEY")
CONSUMER_SECRET = os.getenv("TWITTER_API_SECRET_KEY")
ACCESS_TOKEN = os.getenv("TWITTER_ACESS_TOKEN")
ACCESS_TOKEN_SECRET = os.getenv("TWITTER_ACESS_TOKEN_SECRET")
TWITTER_BEARER_TOKEN = os.getenv("TWITTER_BEARER_TOKEN")
TWITTER_CLIENT_ID = os.getenv("TWITTER_CLIENT_ID")
TWITTER_CLIENT_SECRET = os.getenv("TWITTER_CLIENT_SECRET")
# Create an OAuth1Session instance with your keys and tokens
twitter_auth = OAuth1Session(CONSUMER_KEY, client_secret=CONSUMER_SECRET,
resource_owner_key=ACCESS_TOKEN,
resource_owner_secret=ACCESS_TOKEN_SECRET)
url = "https://api.twitter.com/2/tweets"
payload = json.dumps({
"text": "HelloCertainly, here's the continuation of the translation in HTML format:
html
Copy code
And we can add the URLs it requests in case we need to configure a Webhook:

Install this library with Python:
pip install requests_oauthlib
We also need to install this one to read the .env:
pip install python-dotenv
Now, let's go to a text editor and create this file:
import os
from dotenv import load_dotenv
import json
import oauthlib.oauth1
from requests_oauthlib import OAuth1Session
# Load environment variables from a .env file
load_dotenv()
CONSUMER_KEY = os.getenv("TWITTER_API_KEY")
CONSUMER_SECRET = os.getenv("TWITTER_API_SECRET_KEY")
ACCESS_TOKEN = os.getenv("TWITTER_ACESS_TOKEN")
ACCESS_TOKEN_SECRET = os.getenv("TWITTER_ACESS_TOKEN_SECRET")
TWITTER_BEARER_TOKEN = os.getenv("TWITTER_BEARER_TOKEN")
TWITTER_CLIENT_ID = os.getenv("TWITTER_CLIENT_ID")
TWITTER_CLIENT_SECRET = os.getenv("TWITTER_CLIENT_SECRET")
# Create an OAuth1Session instance with your keys and tokens
twitter_auth = OAuth1Session(CONSUMER_KEY, client_secret=CONSUMER_SECRET,
resource_owner_key=ACCESS_TOKEN,
resource_owner_secret=ACCESS_TOKEN_SECRET)
url = "https://api.twitter.com/2/tweets"
payload = json.dumps({
"text": "Hello World!"
})
# Make the request to Twitter using the OAuth1Session instance
response = twitter_auth.post(url, data=payload, headers={'Content-Type': 'application/json'})
# Print the response
print(response.text)
And add this code. We need to create a .env file with the variables that contain the codes of the created app:
TWITTER_API_KEY = fasdfads
TWITTER_API_SECRET_KEY =
TWITTER_ACESS_TOKEN =
TWITTER_ACESS_TOKEN_SECRET =
TWITTER_BEARER_TOKEN =
TWITTER_CLIENT_ID =
TWITTER_CLIENT_SECRET =