Using and generating Bearer Tokens

A Bearer Token is one of the forms of credential that can be used to access the Twitter API, and is one of the core features of OAuth 2.0. Since it is generated using just an App's API Keys, it makes a request on behalf of that App, and not on behalf of a user (see OAuth 1.0a User Context to make a request on behalf of a user). This is why we call this application-only authentication, as opposed to user-context authentication.

You must pass this token with any endpoint request that can accepts OAuth 2.0 Bearer Token, which you can identify from their API Reference page. To do so, include the following header in your request and making sure to replace $BEARER_TOKEN with your generated Bearer Token:

'Authorization: Bearer $BEARER_TOKEN'

If you were to add this into a cURL request, it would look something like this. Just replace $BEARER_TOKEN and send this request in your command line tool:

      curl --request GET \
  --url 'https://api.twitter.com/2/tweets?ids=1263150595717730305' \
  --header 'Authorization: Bearer $BEARER_TOKEN'
    

You can generate a Bearer Token two different ways:

  • From within the developer portal - see "How to generate from the developer portal" below 
  • By using the POST oauth2/token endpoint - see "How to generate using POST oauth2/token" below 
     

Prerequisites

You will need an approved developer account and must have created a developer App. If you would like to use the new Twitter API v2 endpoints, you will need to make sure that the developer App is within a Project

How to generate from the developer portal

  1. Login to your Twitter account on developer.twitter.com.
  2. Navigate to the "Projects and Apps overview" page.
  3. Click on the key icon of one of your developer Apps to open the "keys and tokens" page.
  4. Under the "Authentication tokens" section, click "Generate" next to Bearer Token. 
     

Once generated, make sure to save your Bearer Token in your password manager, or by passing the following in your terminal to store it as an environment variable (making sure to replace $BEARER_TOKEN with your newly generated token):
 

      export BEARER_TOKEN='$BEARER_TOKEN'
    


How to generate using POST oauth2/token

This option is a little more complicated than the first, but can be used to programmatically generate your Bearer Token. 

  1. Login to your Twitter account on developer.twitter.com.
  2. Navigate to the "Projects and Apps overview" page.
  3. Click on the key icon of one of your developer Apps to open the "keys and tokens" page.
  4. Under the "Authentication tokens" section, retrieve your API Key and API Secret Key.
  5. Copy the following cURL request into your command line tool after replacing $API_KEY and $API_SECRET_KEY with the keys from step 4.
      curl --user "$API_KEY:$API_SECRET_KEY" \
  --data 'grant_type=client_credentials' \
  'https://api.twitter.com/oauth2/token'
    


Here is what the response will look like, assuming you're request was successful. Note that this is a decommissioned Bearer Token:

      {"token_type":"bearer","access_token":"AAAAAAAAAAAAAAAAAAAAAMLheAAAAAAA0%2BuSeid%2BULvsea4JtiGRiSDSJSI%3DEUifiRBkKG5E2XzMDjRfl76ZC9Ub0wnz4XsNiRVBChTYbJcE3F"}
    

Once generated, make sure to save your Bearer Token in your password manager, or by passing the following in your terminal to store it as an environment variable (making sure to replace $BEARER_TOKEN with your newly generated token):

      export BEARER_TOKEN='$BEARER_TOKEN'
    
Was this document helpful?
Thank you

Thank you for the feedback. We’re really glad we could help!

Thank you for the feedback. How could we improve this document?
Thank you for the feedback. Your comments will help us improve our documents in the future.