<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: tlouarn</title>
    <description>The latest articles on DEV Community by tlouarn (@tlouarn).</description>
    <link>https://dev.to/tlouarn</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F602580%2F5f567570-3f9e-4d5d-9425-a74293e3762c.png</url>
      <title>DEV Community: tlouarn</title>
      <link>https://dev.to/tlouarn</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tlouarn"/>
    <language>en</language>
    <item>
      <title>Getting started with the Twitter API using Python</title>
      <dc:creator>tlouarn</dc:creator>
      <pubDate>Tue, 10 Aug 2021 16:56:30 +0000</pubDate>
      <link>https://dev.to/tlouarn/getting-started-with-the-twitter-api-using-python-3i54</link>
      <guid>https://dev.to/tlouarn/getting-started-with-the-twitter-api-using-python-3i54</guid>
      <description>&lt;p&gt;In this article I will get you setup and ready to go with the Twitter API using Python in 8 steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a Twitter account (if you don't already have one)&lt;/li&gt;
&lt;li&gt;Apply for a developer account&lt;/li&gt;
&lt;li&gt;A word about credentials and versioning&lt;/li&gt;
&lt;li&gt;Create your project and your app&lt;/li&gt;
&lt;li&gt;Generate consumer keys&lt;/li&gt;
&lt;li&gt;Generate access tokens&lt;/li&gt;
&lt;li&gt;Install tweepy&lt;/li&gt;
&lt;li&gt;Post your first automated tweet&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Step 1: Create a Twitter account
&lt;/h2&gt;

&lt;p&gt;If you don't already have one, or if you don't want to develop on your existing account, you can create a new Twitter account.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tip 1: You can share the same phone number with up to 10 accounts.&lt;/li&gt;
&lt;li&gt;Tip 2: If you have a Gmail address, you can reuse the same address over multiple accounts by adding "dots" in your email address (see &lt;a href="https://support.google.com/mail/answer/7436150?hl=en-GB"&gt;here&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 2: Apply for a developer account
&lt;/h2&gt;

&lt;p&gt;Developer accounts are regular accounts on steroids.&lt;/p&gt;

&lt;p&gt;Go to &lt;a href="https://developer.twitter.com/en/apply-for-access"&gt;https://developer.twitter.com/en/apply-for-access&lt;/a&gt; and follow the steps to create a developer account attached to your main Twitter account.&lt;/p&gt;

&lt;p&gt;You will have to answer a few questions such as the purpose of the applications you intend to develop as well as the type of analytics you are planning to do with Twitter data.&lt;/p&gt;

&lt;p&gt;Unless you are into really weird stuff, your developer account should be immediately validated.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: A word about credentials and versioning
&lt;/h2&gt;

&lt;p&gt;The documentation can be confusing, so here is a quick summary regarding credentials and versioning.&lt;/p&gt;

&lt;p&gt;Twitter uses an authentication protocol called OAuth (in fact, Twitter even helped to create it, but that's another story).&lt;/p&gt;

&lt;p&gt;OAuth exists in different versions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;OAuth 1.0a: you will use this protocol to act on behalf of a Twitter account via the API (e.g. post tweets, like tweets, DM people etc.)&lt;/li&gt;
&lt;li&gt;OAuth 2.0: you may have the option of using this protocol for the application's own read-only actions (e.g. lookup a user, load tweets etc.)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Furthermore, the API itself exists in two versions: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the legacy API v1 &lt;/li&gt;
&lt;li&gt;the newly baked API v2 which is only being released bit by bit. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Not all endpoints have been migrated yet, far from it (you can follow the changelog &lt;a href="https://developer.twitter.com/en/updates/changelog"&gt;here&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;To summarize, depending on the endpoint you wish to call, you may use the API v1 or the API v2, and depending on the nature of the operation you wish to execute, you will be using OAuth 1.0a or you may have the option of using OAuth 2.0.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Create your project and your app
&lt;/h2&gt;

&lt;p&gt;The notion of "project" was introduced with Twitter API v2: in order to access endpoints from API v2, your app needs to be part of a project.&lt;/p&gt;

&lt;p&gt;The rules are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Only 1 Standard Project per developer account (you can apply for an Academic Research Project as well)&lt;/li&gt;
&lt;li&gt;Only 1 app per project (Twitter is planning on allowing several apps per project)&lt;/li&gt;
&lt;li&gt;The app name needs to be unique&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once your developer account has been validated, you should see the prompts for a project name and an app name (remember: your app name needs to be unique).&lt;/p&gt;

&lt;p&gt;Let your creativity speak!&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Generate consumer keys
&lt;/h2&gt;

&lt;p&gt;Right after you chose your app name, you will be presented with your keys and tokens like below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--81zhf2mU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n3x6zucx04g4074wz3vg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--81zhf2mU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n3x6zucx04g4074wz3vg.png" alt="Keys and Tokens"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Please save them somewhere secure, and once that's done, save them again. This is the only time you will see them.&lt;/p&gt;

&lt;p&gt;The API Key and API Secret Key (also known as Consumer Keys) will authenticate the app itself with OAuth 1.0a.&lt;/p&gt;

&lt;p&gt;The Bearer Token can be used with OAuth 2.0 but we will not use it in this tutorial.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 6: Generate access tokens
&lt;/h3&gt;

&lt;p&gt;Just before we generate the access tokens, we need to give Read and Write access to our app in order to be able to post tweets. &lt;/p&gt;

&lt;p&gt;From the main Settings page, find the App permissions section and click on Edit:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oSZXQSYW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0cw9npsf4tdjzdtqt9rs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oSZXQSYW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0cw9npsf4tdjzdtqt9rs.png" alt="Edit App permissions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then select "Read and Write":&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GZccbZpe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6cihv6z8u17zx7ld8ka4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GZccbZpe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6cihv6z8u17zx7ld8ka4.png" alt="Change App permissions to Read and Write"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once this is done, go back to the Keys and tokens page and generate your Access Token and Secret.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EmxNQHJi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5smrhxswce28cfllrv93.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EmxNQHJi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5smrhxswce28cfllrv93.png" alt="Generate Access Token and Secret"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You know the drill: save them somewhere secure, and once this is done, save them again. &lt;/p&gt;

&lt;p&gt;Since we gave Read and Write permissions to our app prior to generating the Access Token and Secret, we will be able to post tweets using the API.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 7: Install tweepy
&lt;/h3&gt;

&lt;p&gt;There are several Python clients that you could use to interact with the Twitter API.&lt;/p&gt;

&lt;p&gt;In this tutorial we chose &lt;a href="https://www.tweepy.org/"&gt;tweepy&lt;/a&gt; but feel free to explore the alternatives: the list of libraries is available &lt;a href="https://developer.twitter.com/en/docs/twitter-api/tools-and-libraries"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;From the Terminal, install tweepy using pip.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install tweepy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 8: Post your first automated tweet
&lt;/h3&gt;

&lt;p&gt;Below is the Python code to post a tweet using tweepy. &lt;/p&gt;

&lt;p&gt;Quick reminder on how to fill the XXXXXXXXXX:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The &lt;em&gt;consumer key&lt;/em&gt; and the &lt;em&gt;consumer secret&lt;/em&gt; were created at the beginning of the tutorial: they will allow your script to be authenticated by the platform.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;em&gt;access token&lt;/em&gt; and &lt;em&gt;access token secret&lt;/em&gt; were created in a second step after we changed the app's permissions: they will allow your script to tweet on behalf of your main account.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;tweepy&lt;/span&gt;

&lt;span class="n"&gt;consumer_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'XXXXXXXXXX'&lt;/span&gt;
&lt;span class="n"&gt;consumer_secret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'XXXXXXXXXX'&lt;/span&gt;
&lt;span class="n"&gt;auth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tweepy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OAuthHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;consumer_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;consumer_secret&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;access_token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'XXXXXXXXXX'&lt;/span&gt;
&lt;span class="n"&gt;access_token_secret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'XXXXXXXXXX'&lt;/span&gt;
&lt;span class="n"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_access_token&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;access_token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;access_token_secret&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;api&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tweepy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;API&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;update_status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Hello World!'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the code, then head over to your main Twitter account and &lt;em&gt;voilà!&lt;/em&gt; you should see the tweet. &lt;/p&gt;

&lt;p&gt;Well done!&lt;/p&gt;

</description>
      <category>python</category>
      <category>twitter</category>
      <category>api</category>
      <category>howto</category>
    </item>
  </channel>
</rss>
