from dotenv import load_dotenv
from requests_oauthlib import OAuth1Session
# Load environment variables from a .env file
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"
"text": "HelloCertainly, here's the continuation of the translation in HTML format:
<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>
<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
from requests_oauthlib import OAuth1Session
# Load environment variables from a .env file
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"
# Make the request to Twitter using the OAuth1Session instance
response = twitter_auth.post(url, data=payload, headers={'Content-Type': 'application/json'})
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_ACESS_TOKEN_SECRET =
TWITTER_CLIENT_SECRET =</pre>
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>
<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_ACESS_TOKEN_SECRET =
TWITTER_CLIENT_SECRET =</pre>
Please let me know if you need further translation or assistance with anything else.html<br>
<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>
<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>
<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
<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>
<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>
<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>
<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>
<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>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
from requests_oauthlib import OAuth1Session
# Load environment variables from a .env file
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"
# Make the request to Twitter using the OAuth1Session instance
response = twitter_auth.post(url, data=payload, headers={'Content-Type': 'application/json'})
<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_ACESS_TOKEN_SECRET =
<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>
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>