avatarJosep Ferrer

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3986

Abstract

if not, then we will need them again :(</p><figure id="2f11"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*VlSV2z1p_luSPGL0wi-gTA.png"><figcaption>We get the API keys.</figcaption></figure><p id="f708">All three elements are:</p><ul><li><b>API Key: </b>It is a unique identifier that is usually generated by an API provider and provided to a developer to access their API.</li><li><b>API Key Secret: </b>It is used in combination with the API Key to securely sign API requests. The API Key Secret should be kept private and never shared.</li><li><b>Bearer Token:</b> A bearer token is a security token that is issued by an authorization server and is used to access a protected resource. The token is usually a JSON Web Token (JWT) that contains information about the authenticated user and authorization claims.</li></ul><h1 id="3066">#Step 4: Unlock the power of the Twitter API with your API Keys.</h1><p id="6bc4">To make API requests, you’ll need to generate API keys for your Twitter Developer App. Simply go to the <code>Keys and Tokens</code>section and click on the key icon.</p><figure id="69f9"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*YTadFr7m-vws83oz6C8YYQ.png"><figcaption>Twitter for developers dashboard view.</figcaption></figure><p id="ddc4">In the following view, you need to click into generate.</p><figure id="39b7"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*fkhfJikK2grmvXRuppd94Q.png"><figcaption>Creating the consumer keys to access our APP.</figcaption></figure><p id="afd3">Now we need to copy them again and store them somewhere safe — especially when sharing our code with others! :)</p><figure id="84ae"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*JW24j_iVgt84eKiZuCu8rg.png"><figcaption>Our Twitter APP access keys</figcaption></figure><p id="c401">What’s the main problem? <b>The app we just created is read_only.</b> This is why we need to change this. How? We first go back to our main view with all our apps.</p><figure id="4d6b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*FoQ4-RlIEu3ptjnKvZBm0A.png"><figcaption>Twitter for developers main view</figcaption></figure><p id="4dfc">Then we click into the setting button of our just-created app.</p><figure id="40ac"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*ooYGin19Es-N-pJjxmU6Bg.png"><figcaption>App settings view</figcaption></figure><p id="1730">Now we need to scroll down until User authentication settings</p><figure id="25d1"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*LgPoUVvQosCIyjmuQoMbjg.png"><figcaption>App settings view.. User authentication settings.</figcaption></figure><p id="687a">Finally, to change the permission, we need to set the following options:</p><ul><li>Choose the app to be <b>Read and write</b></li><li>Choose the app to be<b> Web App, Automated App or Bot</b></li><li>Define <a href="https://twitter.com/rfeers_medium"><b>https://twitter.com/</b></a><b>@username </b>as Call Back URL</li></ul><figure id="bb91"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*cDoWdWcX6naVZQF09EdkSQ.png"><figcaption>Changing our user authentication settings.</figcaption></figure><p id="d567">And now we are all set up! :D</p><h1 id="1147">#Step 5: Start sending API requests and bring your data to life!</h1><p id="44a8">With your API keys, you’re ready to start making API requests using RESTful API calls. <b>These are simple HTTP requests to retrieve data from the Twitter API.</b></p><p id="c750">You can use tools like Postman or curl to make these requests. However, I will be using Python. For Python, we need to import the Tweepy library. If you need to install it, it is <code>pip install tweepy</code>.</p><p id="a721">Then we just need to define our API and access keys and define the connection with twitter.</p> <figure id="584d"> <div> <div>

Options

          <iframe class="gist-iframe" src="/gist/rfeers/17af348bd3efb7db0f7f8d516def2ef8.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="ddfc">Now you might be wondering, what can I do with this? Here we go with two examples!</p><h2 id="d512">#Checking our public tweets</h2><p id="bf13">You can check all your posted tweets with the simple following code.</p>
    <figure id="cf69">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/rfeers/a8a5ec67e93301caafc1bc549c118eef.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="5bfd">And you can observe in your python terminal all your historical tweets. Ad you can guess, I am using a dummy account for this article — so there are not many nor interesting tweets.</p><figure id="e4d9"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*uFi1x68D3I3BlYEPzPXl4w.png"><figcaption>All tweets of my time line displayed.</figcaption></figure><h2 id="df52">#Writing a tweet directly</h2><p id="86f7">You can post new tweets using directly python.</p>
    <figure id="3ca0">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/rfeers/d83b368385a7f5d52b115633636d3b0b.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="dee9">And if we go check our Twitter account, we will see the tweet is already posted!</p><figure id="dd54"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*nLEERwMSr_f3o_Tvg6pWlw.png"><figcaption></figcaption></figure><p id="0668">Hope you find this article useful! These are the basics to get started with the Twitter API. I will be posting more articles explaining the functionalities of the API — and how to get the advantage of it. So stay tuned! ;)</p><p id="7024"><b>Data always has a better idea — trust it.</b></p><p id="c019">You can suscribe to my <a href="https://medium.com/subscribe/@rfeers"><b>Medium Newsletter</b></a><b> to stay tuned and receive my content</b>. <i>I promise it will be unique!</i></p><p id="80b0">If you are not a full Medium member yet, <b>just check it out <a href="https://medium.com/@rfeers/membership">here</a> to support me and many other writers. </b><i>It really helps </i>:D</p><p id="6534">Some other nice medium related articles you should go check out! :D</p><div id="de03" class="link-block">
      <a href="https://readmedium.com/creating-the-biggest-climate-temperature-dataset-9136cafa4298">
        <div>
          <div>
            <h2>Creating the biggest climate temperature dataset in 7 minutes</h2>
            <div><h3>Using Berkeley Earth data, python and beautifulsoup</h3></div>
            <div><p>medium.com</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*drpUOt0MsdkOYw3345R75w.png)"></div>
          </div>
        </div>
      </a>
    </div><div id="faaa" class="link-block">
      <a href="https://readmedium.com/10-useful-github-repositories-every-data-scientist-should-bookmark-241fcdda7833">
        <div>
          <div>
            <h2>10 useful Github repositories every Data Scientist should bookmark</h2>
            <div><h3>And where to find them</h3></div>
            <div><p>medium.com</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*d2kiCm35WNom3pICK4rndw.png)"></div>
          </div>
        </div>
      </a>
    </div></article></body>

Getting started with the Twitter API in 2023

And how to connect your Twitter account via Python

Self-made image

Have you ever wanted to bring your Twitter data to life in your own custom app? Or have you ever thought about automating tweet posts?

With the Twitter API, you can do just that!

But, where do you start? No worries, I’ve got you covered!

Let’s discover how together 👇🏻

Table of contents #Step 1: Join the Twitter community #Step 2: Get your hands on a Twitter Developer Account. #Step 3: Time to make your first Twitter Developer App. #Step 4: Unlock the power of the Twitter API with your API Keys. #Step 5: Start sending API requests and bring your data to life! #Checking our public tweets#Writing a tweet directly

#Step 1: Join the Twitter community

If you don’t already have a Twitter account, create one. Head over to Twitter and sign up in a few simple clicks. If you don’t want to… then maybe this is not your article…

Twitter landing page

#Step 2: Get your hands on a Twitter Developer Account.

To access the Twitter API, you need to apply for a developer account. This can be done on developer.twitter.com. Just click on Apply and fill out some information about your app and how you plan to use the Twitter API. It usually takes 2 days to get verified — so you should start now!

Twitter for developers landing page

#Step 3: Time to make your first Twitter Developer App.

Once you’re approved, it’s time to start using your twitter developer account.

Twitter for developers landing page

If you click into the Developer Portal, you will be led into your main view:

Twitter for developers main view

As you can observe, I have already two apps created. However, I am going to create a brand new one with you :)

First of all, we need to create a project. Mine is called Medium. We add an app and choose to create a new one.

Twitter for developers page. We select to create a new app.

Then you need to choose the app environment. In my case, I usually choose the Production one. You can choose whatever you feel comfier with.

Twitter for developers page. Choosing the environment of our app.

You need to pick the new name

And finally, you got the API Key, API Key secret, and Bearer Token. You need to store them three, if not, then we will need them again :(

We get the API keys.

All three elements are:

  • API Key: It is a unique identifier that is usually generated by an API provider and provided to a developer to access their API.
  • API Key Secret: It is used in combination with the API Key to securely sign API requests. The API Key Secret should be kept private and never shared.
  • Bearer Token: A bearer token is a security token that is issued by an authorization server and is used to access a protected resource. The token is usually a JSON Web Token (JWT) that contains information about the authenticated user and authorization claims.

#Step 4: Unlock the power of the Twitter API with your API Keys.

To make API requests, you’ll need to generate API keys for your Twitter Developer App. Simply go to the Keys and Tokenssection and click on the key icon.

Twitter for developers dashboard view.

In the following view, you need to click into generate.

Creating the consumer keys to access our APP.

Now we need to copy them again and store them somewhere safe — especially when sharing our code with others! :)

Our Twitter APP access keys

What’s the main problem? The app we just created is read_only. This is why we need to change this. How? We first go back to our main view with all our apps.

Twitter for developers main view

Then we click into the setting button of our just-created app.

App settings view

Now we need to scroll down until User authentication settings

App settings view.. User authentication settings.

Finally, to change the permission, we need to set the following options:

  • Choose the app to be Read and write
  • Choose the app to be Web App, Automated App or Bot
  • Define https://twitter.com/@username as Call Back URL
Changing our user authentication settings.

And now we are all set up! :D

#Step 5: Start sending API requests and bring your data to life!

With your API keys, you’re ready to start making API requests using RESTful API calls. These are simple HTTP requests to retrieve data from the Twitter API.

You can use tools like Postman or curl to make these requests. However, I will be using Python. For Python, we need to import the Tweepy library. If you need to install it, it is pip install tweepy.

Then we just need to define our API and access keys and define the connection with twitter.

Now you might be wondering, what can I do with this? Here we go with two examples!

#Checking our public tweets

You can check all your posted tweets with the simple following code.

And you can observe in your python terminal all your historical tweets. Ad you can guess, I am using a dummy account for this article — so there are not many nor interesting tweets.

All tweets of my time line displayed.

#Writing a tweet directly

You can post new tweets using directly python.

And if we go check our Twitter account, we will see the tweet is already posted!

Hope you find this article useful! These are the basics to get started with the Twitter API. I will be posting more articles explaining the functionalities of the API — and how to get the advantage of it. So stay tuned! ;)

Data always has a better idea — trust it.

You can suscribe to my Medium Newsletter to stay tuned and receive my content. I promise it will be unique!

If you are not a full Medium member yet, just check it out here to support me and many other writers. It really helps :D

Some other nice medium related articles you should go check out! :D

Programming
Software Development
Python
Twitter
Data Science
Recommended from ReadMedium