Kiyana
Kiyana

Reputation: 43

I am trying to get recent tweets from specific users using twitters API on Postman but am having trouble with OAuth2

I am trying to use Postman to do a get request to get tweets from a specific User. I've followed Twitters documentation and created a Bearer Token with Postman but I am still receiving a 400 error. There are no details to this error it just say bad request.

Is the Client Id and Client Secret = My Consumer Api Key and my Consumer Api Key Secret? Or is this my Access Token and Access Token Secret?

Postman Oauth2

I am then making a get request to https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterdev&count=1

Upvotes: 2

Views: 850

Answers (1)

EricWasTaken
EricWasTaken

Reputation: 1717

Once you have a Bearer token, the process is much simpler than what you show in your question.

To make it easier for others to follow this answer, below is the complete process.

You've done some of this already, so feel free to skip to MAKE A REQUEST TO TWITTER WITH A BEARER TOKEN USING POSTMAN.

PRE-REQUISITES

Note: I am using Postman 7.1.1, but I know for a fact that all of what I show here is possible with prior versions as well (though the labels for Authorization, etc might be slightly different.)

For those that might want to follow along but are not as far along as the user asking the question, here is how to catch up:

  1. Sign up for a Twitter developer account. (https://developer.twitter.com - and sign in with your existing Twitter account, then proceed to apply. There's now an application process.)
  2. Once you have a developer account, create a new App. (https://developer.twitter.com/en/apps).
  3. Once you have an App, go to the App's DETAILS then KEYS AND TOKENS page and take note of the "API secret key" and "API secret key".
  4. Finally, generate a bearer token for the app. (Steps for how to do that in Postman below, or follow the instructions from Twitter to do this via CURL: https://developer.twitter.com/en/docs/basics/authentication/guides/bearer-tokens).
    • Note that it's not clear from the documentation how long this Bearer Token will be valid for. It's possible (and likely) that you'll need to regenerate a bearer token for your application from time to time.

GENERATE A BEARER TOKEN FOR YOUR APP USING POSTMAN

Create a postman request as follows: BEARER TOKEN REQUEST

Let's call out the various parts of this request:

  1. The request must be a POST request.
  2. The request URL is https://api.twitter.com/oauth2/token?grant_type=client_credentials
  3. Click on the Authorization tab.
  4. Select Basic Auth for the authorization type.
  5. Enter your Twitter API key in the Postman Username field. You noted this value when you created your app.
  6. Enter your Twitter API secret key in the Postman Password field. You noted this value when you created your app.
  7. Click on Preview Request which will take the authorization values and add the proper headers for the request. (The request won't work if you don't do this as it won't have the proper authorization header!)

Once you have it set up like the above, Send this request and note the response, which should be in the form:

{
    "token_type": "bearer",
    "access_token": "AAAAAAAAAAAAAAAAAAAAAE3Q%SOMEoLONGoTOKENoHEREoHEREoHEREoHEREoLxTx4xddH50oSOMEoLONGoTOKENoHEREoHEREoHEREoHEREoPxsd3x"
}

(The above is not a real token and the words are for illustration only. Your token will be gibberish.)

MAKE A REQUEST TO TWITTER WITH A BEARER TOKEN USING POSTMAN

With all of the preparation out of the way, we're ready for the direct answer to @Kiyana's question.

Now that you have a Bearer token making the actual request to an endpoint that supports this authorization type is pretty simple. (Note that not all of Twitter's endpoints might support this. Refer back to the above link on Bearer Tokens for details.)

Create another postman request as follows: USER TIMELINE REQUEST

  1. This request can be a GET request.
  2. The request url is what you noted in your question: https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterdev&count=1
  3. Click on the Authrization tab.
  4. This time select Bearer Token for the type of authorization.
  5. In the Token field enter the value for access_token that you just received back from the bearer token request.
  6. Click Preview Request to have the request's headers properly setup. (The request won't work if you don't do this as it won't have the proper authorization header!)

Send this request and you should indeed receive back the response you're interested in!

Timeline Request Response

USING HEADERS DIRECTLY FOR A BEARER TOKEN

As a bonus answer here, in the examples above, we used the Authorization tab for Postman. However, all that this tab does is that it adds the proper request headers to your request. If you don't want to use the Authorization tab you can just add headers to your request instead!

I won't get into the details of how to do the first one (the Basic Auth with username/password) because it's a little involved.

However, in the second case, the header for a bearer token is very simple. The header that is added is simply called Authorization and the value that is passed there is simply the string "Bearer", plus space then your token.

For example:

Authorization: Bearer AAAAAAAAAAAAAAAAAAAAAE3Q%SOMEoLONGoTOKENoHEREoHEREoHEREoHEREoLxTx4xddH50oSOMEoLONGoTOKENoHEREoHEREoHEREoHEREoPxsd3x

Upvotes: 3

Related Questions