The current guides available online to access the Twitter API's via postman are extremely confusing.
I recently went through the process and am gonna document it here for anyone who is following in my footsteps and just wants to get a simple request through to test out the twitter API's.
Register for a Developer Account
The first step is to register for a developer account here:
It used to be an entire process to get approved for a twitter develope account, but now it's a one click step to sign up your account
Note: this starts you off at the twitter developer free plan (which has limitations)
Twitter API Plans
Here is an overview of the current plans. As you can see, the free tier is limited mostly to "write" use cases, and one "read" api, so you will only be able to call the below API's
Create a Project and App
The next step, is to create a project and app and assign the app to your project in the developer console.
There should already be a default project and app created upon activation of your developer console, but I went ahead and made my own for good measure.
Make sure your app is assigned to your project otherwise, you could get errors like:
"When authenticating requests to the Twitter API v2 endpoints, you must use keys and tokens from a Twitter developer App that is attached to a Project. You can create a project via the developer portal."
Set up User Authentication in Twitter App
We'll need to set up user authentication so that we can successfully authenticate via browser for postman.
Within your app settings, in twitter dev console, you should see this at the bottom:
- App permissions: set this to whatever you want to, I have it as all permissions
- Type of App: I've selected web app, but you can have this as whatever you need
- App Info: Make sure to set this as: https://oauth.pstmn.io/v1/callback
- Website URL: your website url usually (I'm testing so I just set it as the same as callback)
Once you click save, this will generate your:
- App Client ID
- App Client secret
We will need both of these when we are generating the access token in postman! So make sure to store these somewhere safe.
Import Twitter Workspace to Postman App
I tried using the postman webapp (going to https://web.postman.co/), but for some reason it wasn't able to get past the authenticate callback step so I ended up using the app to eventually get it working.
More on this in the Gotchas section at the bottom.
Download the postman app to get started:
Once you've downloaded the postman app, you can search(try "twitter workspace or https://t.co/twitter-api-postman") for the twitter public workspace:
Select the workspace, and then "fork" it so that you can start making requests. Currently postman doesn't let you run requests on public workspaces so forking it gives you your own version of the workspace that you can make changes to:
Once you fork it, you should now see the Twitter API v2 collection in "My Workspace"
View AUTH requirements for the desired API
Before we start the authentication process in Postman, we have to check to see what auth requirements are needed by our API, along with the scope permissions that we need.
The supported authentication methods is circled above, as we can see from the screenshot this API (available for free tier) supports both:
- OAuth 2.0 Authorization Code with PKCE (recommended)
- OAuth 1.0a
We'll be using the first method, since it seems like twitter is recommending it.
Additionally, we'll need to use the Oauth 2.0 scopes for our next steps so we'll take note of the scope: "tweet.read users.read" (we'll need both)
Getting User Access Tokens with Postman
Now that we know what Authentication Method to use along with the scope we can start the authentication via browser process.
- Select the "Authorization" tab
- For Type, select "OAuth 2.0"
Next go to the Configure New Token section
- In “Configuration Options” tab, you can update the “Grant Type” to “Authorization Code (With PKCE)”
Now you should see some fields appear, fill them out accordingly
- Token Name: <Set your custom token name>
- Auth URL: https://twitter.com/i/oauth2/authorize
- Access Token URL - https://api.twitter.com/2/oauth2/token
- Client ID: The client ID value we created previously (set up user authentication step)
- Client Secret: The client secret we created previously (set up user authentication step)
- Update Scope: tweet.read users.read (Scope should match the endpoints you want to connect to, refer to documentation for this value usually)
- Your callback URL (also known as the redirect URL). This must match what you have in your App's authentication settings: "https://oauth.pstmn.io/v1/callback" (This is preset by postman specifically for authorizing via the browser)
- State - state
Once we have this all filled out, you can click "Get New Access Token" and a browser window should open up, asking you to authenticate for your twitter user.
Accept it and then chrome should prompt you to open your postman app in a popup
Sometimes the popup could be blocked by chrome so you will have to enable popups by clicking the popup icon in the right of your browser url bar and "Allow popups"
When we get back to Postman app, we should see the following:
With a new access token generated
We can use this "Access Token" as the bearer token in any api calls that require OAuth 2.0 🎉!
Copy the token!
Make our first Twitter API Call
Now we can navigate to our desired API and make our first call
- Select "User Lookup" > "Authenticated User Lookup"
- Go to the Auth Tab
- Under Type: "Bearer Token"
- Enter the token that we copied from the previous step
- Send command
You should see the name of the account that you authenticated with as the response, if your api call is successful.
Gotchas
Postman Webapp (web.postman.co) not able to authenticate via browser
When I try to run through generating a new access token via the postman webapp I was able to successfully authenticate via twitter, but the process gets stuck waiting for the callback to my browser. To avoid this use the postman app (handles callbacks and redirects better)
This step was successful:
Then it got stuck here and never authenticated:
Into the weeds: for some reason the postman webapp was sending this request which always failed