Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Publishing a Tweet using Twitter API V2 and Python

Tiempo de lectura: 3 minutos

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:

  1. 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:

  1. 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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
pip install requests_oauthlib
pip install requests_oauthlib
pip install requests_oauthlib

We also need to install this one to read the .env:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
pip install python-dotenv
pip install python-dotenv
pip install python-dotenv

Now, let’s go to a text editor and create this file:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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
<p>And we can add the URLs it requests in case we need to configure a Webhook:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><img decoding="async" loading="lazy" src="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-37.png?resize=504%2C276&ssl=1" alt="" class="wp-image-6063" style="width:504px;height:276px" width="504" height="276" srcset="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-37.png?w=826&ssl=1 826w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-37.png?resize=300%2C165&ssl=1 300w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-37.png?resize=768%2C421&ssl=1 768w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-37.png?resize=212%2C116&ssl=1 212w" sizes="auto, (max-width: 504px) 100vw, 504px" data-recalc-dims="1"></figure>
</div>
<p>Install this library with Python:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pip install requests_oauthlib</pre>
<p>We also need to install this one to read the .env:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pip install python-dotenv</pre>
<p>Now, let's go to a text editor and create this file:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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)</pre>
<p>And add this code. We need to create a .env file with the variables that contain the codes of the created app:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">TWITTER_API_KEY = fasdfads
TWITTER_API_SECRET_KEY =
TWITTER_ACESS_TOKEN =
TWITTER_ACESS_TOKEN_SECRET =
TWITTER_BEARER_TOKEN =
TWITTER_CLIENT_ID =
TWITTER_CLIENT_SECRET =</pre>
<p><br>
Please note that I have translated the content, but some variable names and placeholders remain unchanged as they appear in the original text.I apologize, but it seems that the content you provided is quite long, and it's hitting the response length limit. To ensure I can provide a complete translation, I'll need to break it into smaller parts. Here's the continuation of the translation in HTML format:<br>
html<br>
Copy code</p>
<p>Now let's add this code, and we need to create a .env file with the variables containing the codes of the created app:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">TWITTER_API_KEY = fasdfads
TWITTER_API_SECRET_KEY =
TWITTER_ACESS_TOKEN =
TWITTER_ACESS_TOKEN_SECRET =
TWITTER_BEARER_TOKEN =
TWITTER_CLIENT_ID =
TWITTER_CLIENT_SECRET =</pre>
<p><br>
Please let me know if you need further translation or assistance with anything else.html<br>
Copy code</p>
<p>Para que podamos acceder a la APP de forma externa, tenemos que configurar los permisos, para ello vamos a User authentication settings:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><img decoding="async" loading="lazy" src="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-34.png?resize=629%2C200&ssl=1" alt="" class="wp-image-6059" style="width:629px;height:200px" width="629" height="200" srcset="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-34.png?w=805&ssl=1 805w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-34.png?resize=300%2C95&ssl=1 300w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-34.png?resize=768%2C244&ssl=1 768w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-34.png?resize=228%2C73&ssl=1 228w" sizes="auto, (max-width: 629px) 100vw, 629px" data-recalc-dims="1"></figure>
</div>
<p>Pulsamos en Set up y configuramos los permisos, en este caso leer y escribir:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><img decoding="async" loading="lazy" src="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-35.png?resize=614%2C341&ssl=1" alt="" class="wp-image-6061" style="width:614px;height:341px" width="614" height="341" srcset="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-35.png?w=831&ssl=1 831w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-35.png?resize=300%2C166&ssl=1 300w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-35.png?resize=768%2C426&ssl=1 768w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-35.png?resize=209%2C116&ssl=1 209w" sizes="auto, (max-width: 614px) 100vw, 614px" data-recalc-dims="1"></figure>
</div>
<p>También podemos elegir el tipo de APP:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><img decoding="async" loading="lazy" src="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-36.png?resize=468%2C191&ssl=1" alt="" class="wp-image-6062" style="width:468px;height:191px" width="468" height="191" srcset="https://i0html
Copy code
<p>Para que podamos acceder a la APP de forma externa, tenemos que configurar los permisos, para ello vamos a User authentication settings:</p>
<div class=" wp-block-image"="">
<figure class="aligncenter size-full is-resized"><img decoding="async" loading="lazy" src="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-34.png?resize=629%2C200&ssl=1" alt="" class="wp-image-6059" style="width:629px;height:200px" width="629" height="200" srcset="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-34.png?w=805&ssl=1 805w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-34.png?resize=300%2C95&ssl=1 300w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-34.png?resize=768%2C244&ssl=1 768w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-34.png?resize=228%2C73&ssl=1 228w" sizes="auto, (max-width: 629px) 100vw, 629px" data-recalc-dims="1"></figure>
</figure></div>
<p>Pulsamos en Set up y configuramos los permisos, en este caso leer y escribir:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><img decoding="async" loading="lazy" src="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-35.png?resize=614%2C341&ssl=1" alt="" class="wp-image-6061" style="width:614px;height:341px" width="614" height="341" srcset="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-35.png?w=831&ssl=1 831w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-35.png?resize=300%2C166&ssl=1 300w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-35.png?resize=768%2C426&ssl=1 768w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-35.png?resize=209%2C116&ssl=1 209w" sizes="auto, (max-width: 614px) 100vw, 614px" data-recalc-dims="1"></figure>
</div>
<p>También podemos elegir el tipo de APP:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><img decoding="async" loading="lazy" src="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-36.png?resize=468%2C191&ssl=1" alt="" class="wp-image-6062" style="width:468px;height:191px" width="468" height="191" srcset="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-36.png?w=804&ssl=1 804w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-36.png?resize=300%2C122&ssl=1 300w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-36.png?resize=768%2C313&ssl=1 768w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-36.png?resize=228%2C93&ssl=1 228w" sizes="auto, (max-width: 468px) 100vw, 468px" data-recalc-dims="1"></figure>
</div>
<p>Y podemos añadir las URLS que solicita por si necesitamos configurar un Webhook:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><img decoding="async" loading="lazy" src="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-37.png?resize=504%2C276&ssl=1" alt="" class="wp-image-6063" style="width:504px;height:276px" width="504" height="276" srcset="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-37.png?w=826&ssl=1 826w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-37.png?resize=300%2C165&ssl=1 300w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-37.png?resize=768%2C421&ssl=1 768w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-37.png?resize=212%2C116&ssl=1 212w" sizes="auto, (max-width: 504px) 100vw, 504px" data-recalc-dims="1"></figure>
</div>
<p>Instalamos esta librería con Python:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pip install requests_oauthlib</pre>
<p>html<br>
Copy code</p>
<p>También tenemos que instalar esta, para leer el .env:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pip install python-dotenv</pre>
<p>Ahora vamos a un editor de texto y creamos este archivo:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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 instance of OAuth1Session 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)
</pre>
<p>And add this code, and we have to create a .env file with the variables containing the codes of the created app:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">TWITTER_API_KEY = fasdfads
TWITTER_API_SECRET_KEY =
TWITTER_ACESS_TOKEN =
TWITTER_ACESS_TOKEN_SECRET =
TWITTER_BEARER_TOKEN =
TWITTER_CLIENT_ID =
TWITTER_CLIENT_SECRET =
</pre>
<p> </p>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope="" itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img data-recalc-dims="1" decoding="async" src="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/07/cropped-light_logo-5.png?resize=100%2C100&ssl=1" width="100" height="100" alt="" itemprop="image"></div><div class="saboxplugin-authorname"><a href="https://devcodelight.com/en/author/courstube/" class="vcard author" rel="author"><span class="fn">DevCodeLight</span></a></div><div class="saboxplugin-desc"><div itemprop="description"></div></div><div class="clearfix"></div></div></div><div class="post-views content-post post-6432 entry-meta load-static">
<span class="post-views-icon dashicons dashicons-chart-bar"></span> <span class="post-views-label">Post Views:</span> <span class="post-views-count">24</span>
</div> <div class="wpulike wpulike-robeen "><div class="wp_ulike_general_class wp_ulike_is_not_liked"><button type="button" aria-label="Like Button" data-ulike-id="6432" data-ulike-nonce="53e74efc6f" data-ulike-type="post" data-ulike-template="wpulike-robeen" data-ulike-display-likers="0" data-ulike-likers-style="popover" class="wp_ulike_btn wp_ulike_put_image wp_post_btn_6432"></button><span class="count-box wp_ulike_counter_up" data-ulike-counter-value="0">0</span> </div></div>
<div class="sharedaddy sd-sharing-enabled"><div class="robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing"><h3 class="sd-title">Comparte esto:</h3><div class="sd-content"><ul><li class="share-facebook"><a rel="nofollow noopener noreferrer" data-shared="sharing-facebook-6432" class="share-facebook sd-button share-icon" href="https://devcodelight.com/en/publishing-a-tweet-using-twitter-api-v2-and-python/?share=facebook" target="_blank" title="Click to share on Facebook"><span>Facebook</span></a></li><li class="share-x"><a rel="nofollow noopener noreferrer" data-shared="sharing-x-6432" class="share-x sd-button share-icon" href="https://devcodelight.com/en/publishing-a-tweet-using-twitter-api-v2-and-python/?share=x" target="_blank" title="Click to share on X"><span>X</span></a></li><li class="share-end"></li></ul></div></div></div>
<div id="jp-relatedposts" class="jp-relatedposts">
<h3 class="jp-relatedposts-headline"><em>Related</em></h3>
</div>
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 <p>And we can add the URLs it requests in case we need to configure a Webhook:</p> <div class="wp-block-image"> <figure class="aligncenter size-full is-resized"><img decoding="async" loading="lazy" src="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-37.png?resize=504%2C276&ssl=1" alt="" class="wp-image-6063" style="width:504px;height:276px" width="504" height="276" srcset="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-37.png?w=826&ssl=1 826w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-37.png?resize=300%2C165&ssl=1 300w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-37.png?resize=768%2C421&ssl=1 768w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-37.png?resize=212%2C116&ssl=1 212w" sizes="auto, (max-width: 504px) 100vw, 504px" data-recalc-dims="1"></figure> </div> <p>Install this library with Python:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pip install requests_oauthlib</pre> <p>We also need to install this one to read the .env:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pip install python-dotenv</pre> <p>Now, let's go to a text editor and create this file:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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)</pre> <p>And add this code. We need to create a .env file with the variables that contain the codes of the created app:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">TWITTER_API_KEY = fasdfads TWITTER_API_SECRET_KEY = TWITTER_ACESS_TOKEN = TWITTER_ACESS_TOKEN_SECRET = TWITTER_BEARER_TOKEN = TWITTER_CLIENT_ID = TWITTER_CLIENT_SECRET =</pre> <p><br> Please note that I have translated the content, but some variable names and placeholders remain unchanged as they appear in the original text.I apologize, but it seems that the content you provided is quite long, and it's hitting the response length limit. To ensure I can provide a complete translation, I'll need to break it into smaller parts. Here's the continuation of the translation in HTML format:<br> html<br> Copy code</p> <p>Now let's add this code, and we need to create a .env file with the variables containing the codes of the created app:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">TWITTER_API_KEY = fasdfads TWITTER_API_SECRET_KEY = TWITTER_ACESS_TOKEN = TWITTER_ACESS_TOKEN_SECRET = TWITTER_BEARER_TOKEN = TWITTER_CLIENT_ID = TWITTER_CLIENT_SECRET =</pre> <p><br> Please let me know if you need further translation or assistance with anything else.html<br> Copy code</p> <p>Para que podamos acceder a la APP de forma externa, tenemos que configurar los permisos, para ello vamos a User authentication settings:</p> <div class="wp-block-image"> <figure class="aligncenter size-full is-resized"><img decoding="async" loading="lazy" src="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-34.png?resize=629%2C200&ssl=1" alt="" class="wp-image-6059" style="width:629px;height:200px" width="629" height="200" srcset="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-34.png?w=805&ssl=1 805w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-34.png?resize=300%2C95&ssl=1 300w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-34.png?resize=768%2C244&ssl=1 768w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-34.png?resize=228%2C73&ssl=1 228w" sizes="auto, (max-width: 629px) 100vw, 629px" data-recalc-dims="1"></figure> </div> <p>Pulsamos en Set up y configuramos los permisos, en este caso leer y escribir:</p> <div class="wp-block-image"> <figure class="aligncenter size-full is-resized"><img decoding="async" loading="lazy" src="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-35.png?resize=614%2C341&ssl=1" alt="" class="wp-image-6061" style="width:614px;height:341px" width="614" height="341" srcset="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-35.png?w=831&ssl=1 831w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-35.png?resize=300%2C166&ssl=1 300w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-35.png?resize=768%2C426&ssl=1 768w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-35.png?resize=209%2C116&ssl=1 209w" sizes="auto, (max-width: 614px) 100vw, 614px" data-recalc-dims="1"></figure> </div> <p>También podemos elegir el tipo de APP:</p> <div class="wp-block-image"> <figure class="aligncenter size-full is-resized"><img decoding="async" loading="lazy" src="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-36.png?resize=468%2C191&ssl=1" alt="" class="wp-image-6062" style="width:468px;height:191px" width="468" height="191" srcset="https://i0html Copy code <p>Para que podamos acceder a la APP de forma externa, tenemos que configurar los permisos, para ello vamos a User authentication settings:</p> <div class=" wp-block-image"=""> <figure class="aligncenter size-full is-resized"><img decoding="async" loading="lazy" src="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-34.png?resize=629%2C200&ssl=1" alt="" class="wp-image-6059" style="width:629px;height:200px" width="629" height="200" srcset="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-34.png?w=805&ssl=1 805w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-34.png?resize=300%2C95&ssl=1 300w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-34.png?resize=768%2C244&ssl=1 768w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-34.png?resize=228%2C73&ssl=1 228w" sizes="auto, (max-width: 629px) 100vw, 629px" data-recalc-dims="1"></figure> </figure></div> <p>Pulsamos en Set up y configuramos los permisos, en este caso leer y escribir:</p> <div class="wp-block-image"> <figure class="aligncenter size-full is-resized"><img decoding="async" loading="lazy" src="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-35.png?resize=614%2C341&ssl=1" alt="" class="wp-image-6061" style="width:614px;height:341px" width="614" height="341" srcset="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-35.png?w=831&ssl=1 831w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-35.png?resize=300%2C166&ssl=1 300w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-35.png?resize=768%2C426&ssl=1 768w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-35.png?resize=209%2C116&ssl=1 209w" sizes="auto, (max-width: 614px) 100vw, 614px" data-recalc-dims="1"></figure> </div> <p>También podemos elegir el tipo de APP:</p> <div class="wp-block-image"> <figure class="aligncenter size-full is-resized"><img decoding="async" loading="lazy" src="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-36.png?resize=468%2C191&ssl=1" alt="" class="wp-image-6062" style="width:468px;height:191px" width="468" height="191" srcset="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-36.png?w=804&ssl=1 804w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-36.png?resize=300%2C122&ssl=1 300w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-36.png?resize=768%2C313&ssl=1 768w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-36.png?resize=228%2C93&ssl=1 228w" sizes="auto, (max-width: 468px) 100vw, 468px" data-recalc-dims="1"></figure> </div> <p>Y podemos añadir las URLS que solicita por si necesitamos configurar un Webhook:</p> <div class="wp-block-image"> <figure class="aligncenter size-full is-resized"><img decoding="async" loading="lazy" src="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-37.png?resize=504%2C276&ssl=1" alt="" class="wp-image-6063" style="width:504px;height:276px" width="504" height="276" srcset="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-37.png?w=826&ssl=1 826w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-37.png?resize=300%2C165&ssl=1 300w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-37.png?resize=768%2C421&ssl=1 768w, https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/09/imagen-37.png?resize=212%2C116&ssl=1 212w" sizes="auto, (max-width: 504px) 100vw, 504px" data-recalc-dims="1"></figure> </div> <p>Instalamos esta librería con Python:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pip install requests_oauthlib</pre> <p>html<br> Copy code</p> <p>También tenemos que instalar esta, para leer el .env:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pip install python-dotenv</pre> <p>Ahora vamos a un editor de texto y creamos este archivo:</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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 instance of OAuth1Session 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) </pre> <p>And add this code, and we have to create a .env file with the variables containing the codes of the created app:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">TWITTER_API_KEY = fasdfads TWITTER_API_SECRET_KEY = TWITTER_ACESS_TOKEN = TWITTER_ACESS_TOKEN_SECRET = TWITTER_BEARER_TOKEN = TWITTER_CLIENT_ID = TWITTER_CLIENT_SECRET = </pre> <p> </p> <div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope="" itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img data-recalc-dims="1" decoding="async" src="https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/07/cropped-light_logo-5.png?resize=100%2C100&ssl=1" width="100" height="100" alt="" itemprop="image"></div><div class="saboxplugin-authorname"><a href="https://devcodelight.com/en/author/courstube/" class="vcard author" rel="author"><span class="fn">DevCodeLight</span></a></div><div class="saboxplugin-desc"><div itemprop="description"></div></div><div class="clearfix"></div></div></div><div class="post-views content-post post-6432 entry-meta load-static"> <span class="post-views-icon dashicons dashicons-chart-bar"></span> <span class="post-views-label">Post Views:</span> <span class="post-views-count">24</span> </div> <div class="wpulike wpulike-robeen "><div class="wp_ulike_general_class wp_ulike_is_not_liked"><button type="button" aria-label="Like Button" data-ulike-id="6432" data-ulike-nonce="53e74efc6f" data-ulike-type="post" data-ulike-template="wpulike-robeen" data-ulike-display-likers="0" data-ulike-likers-style="popover" class="wp_ulike_btn wp_ulike_put_image wp_post_btn_6432"></button><span class="count-box wp_ulike_counter_up" data-ulike-counter-value="0">0</span> </div></div> <div class="sharedaddy sd-sharing-enabled"><div class="robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing"><h3 class="sd-title">Comparte esto:</h3><div class="sd-content"><ul><li class="share-facebook"><a rel="nofollow noopener noreferrer" data-shared="sharing-facebook-6432" class="share-facebook sd-button share-icon" href="https://devcodelight.com/en/publishing-a-tweet-using-twitter-api-v2-and-python/?share=facebook" target="_blank" title="Click to share on Facebook"><span>Facebook</span></a></li><li class="share-x"><a rel="nofollow noopener noreferrer" data-shared="sharing-x-6432" class="share-x sd-button share-icon" href="https://devcodelight.com/en/publishing-a-tweet-using-twitter-api-v2-and-python/?share=x" target="_blank" title="Click to share on X"><span>X</span></a></li><li class="share-end"></li></ul></div></div></div> <div id="jp-relatedposts" class="jp-relatedposts"> <h3 class="jp-relatedposts-headline"><em>Related</em></h3> </div>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
pip install requests_oauthlib
pip install requests_oauthlib
pip install requests_oauthlib

We also need to install this one to read the .env:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
pip install python-dotenv
pip install python-dotenv
pip install python-dotenv

Now, let's go to a text editor and create this file:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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)
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)
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
TWITTER_API_KEY = fasdfads
TWITTER_API_SECRET_KEY =
TWITTER_ACESS_TOKEN =
TWITTER_ACESS_TOKEN_SECRET =
TWITTER_BEARER_TOKEN =
TWITTER_CLIENT_ID =
TWITTER_CLIENT_SECRET =
TWITTER_API_KEY = fasdfads TWITTER_API_SECRET_KEY = TWITTER_ACESS_TOKEN = TWITTER_ACESS_TOKEN_SECRET = TWITTER_BEARER_TOKEN = TWITTER_CLIENT_ID = TWITTER_CLIENT_SECRET =
TWITTER_API_KEY = fasdfads
TWITTER_API_SECRET_KEY = 
TWITTER_ACESS_TOKEN =
TWITTER_ACESS_TOKEN_SECRET = 
TWITTER_BEARER_TOKEN = 
TWITTER_CLIENT_ID = 
TWITTER_CLIENT_SECRET =


Please note that I have translated the content, but some variable names and placeholders remain unchanged as they appear in the original text.I apologize, but it seems that the content you provided is quite long, and it's hitting the response length limit. To ensure I can provide a complete translation, I'll need to break it into smaller parts. Here's the continuation of the translation in HTML format:
html
Copy code

Now let's add this code, and we need to create a .env file with the variables containing the codes of the created app:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
TWITTER_API_KEY = fasdfads
TWITTER_API_SECRET_KEY =
TWITTER_ACESS_TOKEN =
TWITTER_ACESS_TOKEN_SECRET =
TWITTER_BEARER_TOKEN =
TWITTER_CLIENT_ID =
TWITTER_CLIENT_SECRET =
TWITTER_API_KEY = fasdfads TWITTER_API_SECRET_KEY = TWITTER_ACESS_TOKEN = TWITTER_ACESS_TOKEN_SECRET = TWITTER_BEARER_TOKEN = TWITTER_CLIENT_ID = TWITTER_CLIENT_SECRET =
TWITTER_API_KEY = fasdfads
TWITTER_API_SECRET_KEY = 
TWITTER_ACESS_TOKEN =
TWITTER_ACESS_TOKEN_SECRET = 
TWITTER_BEARER_TOKEN = 
TWITTER_CLIENT_ID = 
TWITTER_CLIENT_SECRET =


Please let me know if you need further translation or assistance with anything else.html
Copy code

Para que podamos acceder a la APP de forma externa, tenemos que configurar los permisos, para ello vamos a User authentication settings:

Pulsamos en Set up y configuramos los permisos, en este caso leer y escribir:

También podemos elegir el tipo de APP:

Pulsamos en Set up y configuramos los permisos, en este caso leer y escribir:

También podemos elegir el tipo de APP:

Y podemos añadir las URLS que solicita por si necesitamos configurar un Webhook:

Instalamos esta librería con Python:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
pip install requests_oauthlib
pip install requests_oauthlib
pip install requests_oauthlib

html
Copy code

También tenemos que instalar esta, para leer el .env:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
pip install python-dotenv
pip install python-dotenv
pip install python-dotenv

Ahora vamos a un editor de texto y creamos este archivo:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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 instance of OAuth1Session 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)
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 instance of OAuth1Session 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)
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 instance of OAuth1Session 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, and we have to create a .env file with the variables containing the codes of the created app:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
TWITTER_API_KEY = fasdfads
TWITTER_API_SECRET_KEY =
TWITTER_ACESS_TOKEN =
TWITTER_ACESS_TOKEN_SECRET =
TWITTER_BEARER_TOKEN =
TWITTER_CLIENT_ID =
TWITTER_CLIENT_SECRET =
TWITTER_API_KEY = fasdfads TWITTER_API_SECRET_KEY = TWITTER_ACESS_TOKEN = TWITTER_ACESS_TOKEN_SECRET = TWITTER_BEARER_TOKEN = TWITTER_CLIENT_ID = TWITTER_CLIENT_SECRET =
TWITTER_API_KEY = fasdfads
TWITTER_API_SECRET_KEY = 
TWITTER_ACESS_TOKEN =
TWITTER_ACESS_TOKEN_SECRET = 
TWITTER_BEARER_TOKEN = 
TWITTER_CLIENT_ID = 
TWITTER_CLIENT_SECRET =

0

Leave a Comment