Using and generating a Bearer Token
A bearer token allows developers to have a more secure point of entry for using the Twitter APIs, and are one of the core features of OAuth 2.0.
Authentication, which uses a Bearer Token, is also known as application-only authentication. A Bearer Token is a byte array of unspecified format that you generate using a script like a curl command. More information about this feature can be found on OAuth's official documentation.
When are they used?
The products that require the use of a Bearer Token are as follows:
- Engagement API
- Premium Search Tweets API
- Account Activity API
- Other APIs that utilize OAuth 2.0 Bearer Token authentication
You will need an approved developer account and to have created a Twitter app. Once you have those, you'll also need to obtain the consumer API keys from the Twitter app detail pages found in the developer portal. Follow the steps below:
- Login to your Twitter account on developer.twitter.com.
- Navigate to the Twitter app dashboard and open the Twitter app for which you would like to generate access tokens.
- Navigate to the "Keys and Tokens" page.
- You'll find the "Consumer API keys" on this page.
How to generate a Bearer Token
Copy the following cURL request into your command line after making changes to the following consumer API keys previously obtained from your Twitter app. Note that the consumer API keys used on this page have been disabled and will not work for real requests.
- API key
- API secret key
<API secret key>e.g.
curl -u '<API key>:<API secret key>' \ --data 'grant_type=client_credentials' \ 'https://api.twitter.com/oauth2/token'
Here's an example of how the curl request should look with your API keys entered:
curl -u 'xvz1evFS4wEEPTGEFPHBog:L8qq9PZyRg6ieKGEKhZolGC0vJWLw8iEJ88DRdyOg' \ --data 'grant_type=client_credentials' \ 'https://api.twitter.com/oauth2/token'
Here is what the response would look like. Note that this is a decommissioned Bearer Token:
Our Bearer Token used to authenticate to resources with OAuth 2.0 would be: