6

I had previously used some jQuery to read tweets on twitter:

$.ajax('https://api.twitter.com/1/statuses/user_timeline.json', {
    crossDomain: true,
    data: {
        screen_name: 'twitterapi',
        count: 5
    },
    dataType: 'jsonp'
}).done(function (tweets) {
    console.log(tweets);
});

As twitter is deprecating their 1.0 API, and requiring OAuth for the 1.1 API, I've been trying to figure out if it's still possible to get tweet data in the same manner.

Simply changing the url to:

https://api.twitter.com/1.1/statuses/user_timeline.json

Results in a 400 Bad Request response with no message.

I know there's a twitter tool to create an OAuth signature for a request, but I'm not sure how to use it with a JSONP request, or even if it can be used with a JSONP request.

Is it still possible in the Twitter 1.1 API to read a user's timeline?

Jimbo
  • 25,790
  • 15
  • 86
  • 131
zzzzBov
  • 174,988
  • 54
  • 320
  • 367

4 Answers4

5

If you take a look at Twitter's Error Codes & Responses, status code 400 means:

The request was invalid. An accompanying error message will explain why. This is the status code will be returned during version 1.0 rate limiting. In API v1.1, a request without authentication is considered invalid and you will get this response.

So while a 400 code used to mean you exceeded the rate limit, now it also returns when the request isn't authenticated.

To authenticate the request, you'd have to add an Oauth Authorization header. There are some libraries that can help with that, but the problem is that to generate the Oauth signature, you'd have to hard-code your app's keys (including secret key) into your client-side code, which will expose it to end-users (not a good idea).

Your best bet is to set up a proxy on your server - have the server make a GET with the Oauth header, and use ajax to get the tweets from your server.

redhotvengeance
  • 27,446
  • 10
  • 49
  • 54
4

redhotvengeance is correct, server side is your only safe option as of March 2013 except that my recommended solution would be to set up a cronjob and cache the results somewhere. Using a proxy is a great way to hit the Rate Limits very quickly!

For example, if you plan to use the user_timeline part of the API you are limited to 15 requests per 15 minutes so if you get more than 60 hits on your page per hour you'll be swapping those 400 errors for 429 errors!

SKWebDev
  • 161
  • 1
  • 5
  • Actually, `user_timeline` has a rate limit of 180 requests per 15 minute interval. But, in genreal, yes - caching is definitely something that you'd want to do to mind the rate limits in nearly every circumstance. – redhotvengeance Jan 11 '13 at 00:09
0

I actually just posted this as an answer to a couple other questions. Sorry about the repetition, but I think this will save you a lot of time:

I just updated a plugin to work with the Twitter 1.1 API. Unfortunately, per Twitter's urging, you will have to perform the actual request from server-side code. However, you can pass the response to the plugin and it will take care of the rest. I don't know what framework you are running, but I have already added sample code for making the request in C#, and will be adding sample code for PHP, shortly.

Good luck! :)

Zachary Kniebel
  • 4,686
  • 3
  • 29
  • 53
0

As stated here regarding a way of being able to get user's timeline with pure client-side JS:

If we want to, we can still parse their pages and reimplement at least a bit of lost functionality with pure client-side JS.

What we need is: a CORS proxy that can deal with HTTPS pages (the only I know is YQL APIs) and the knowledge of how the information is retreived on their public pages. Keeping this in mind, we can fetch, for example, recent 20 tweets from a user, as I did in the demo: http://jsbin.com/agejol/1 (click "Edit in JSBin" to view the code)

P.S. I know this may violate their policy but I care about it just as much as they cared about us when they dropped all their client-side APIs.

Community
  • 1
  • 1
Multiversum
  • 323
  • 3
  • 5
  • That linked post is informative. It would be nice if you could copy over the relevant info into this answer. As it stands this should be a comment not an answer. – zzzzBov Jul 03 '13 at 18:08