3

Recently Twitter updated its API to v1.1 and my program stops working but no matter how much documentation I read, I can't seem to really understand what is needed to make my codes work.

My used-to-work codes are as below :

function getTweets() {

var url = 'http://search.twitter.com/search.json?q=%23yolo&rpp=10&result_type=recent&callback=?';

    $.getJSON(url, function (json) {

        display = [];
        displayDateTime = [];
        if (json.results.length != null) {
            for (var i = 0; i < json.results.length; i++) {
                var time = new Date(json.results[i].created_at);

                display.push("@" + json.results[i].from_user + ": " + json.results[i].text);
                displayDateTime.push("[" + time.toString().substring(0, 19) + "]");

            } //end of for loop

            display.reverse();
            displayDateTime.reverse();

            loadOtherStuffs();
        } //end of if
        else {
            setTimeout(getTweets, 24000);
        }
    });     //end of getJSON
}//end of getTweets()

I tried changing the url to https://api.twitter.com/1.1/search/tweets.json and json.results to json.statuses but it still won't work. It seems that there's a need for oAuth to make this work again but I'm not too sure.

What are exactly the steps to make this work again?

Jimbo
  • 25,790
  • 15
  • 86
  • 131
  • Have you debugged it? On which line is it stopping? – LuigiEdlCarno Jun 20 '13 at 07:54
  • I can't step into anything from $.getJSON onwards. It just highlights everything in it then go to the end of getJSON. It was already like this before. I'm using Visual Studio 2010. – Edeson Lee Yan Wei Jun 20 '13 at 08:23
  • I added some tags, so people with more js knowledge can help you. In the meantime: Are you sure, your url is correct? Did the API update change the get-parameters? Did you check the API change log, to see, what has been changed? – LuigiEdlCarno Jun 20 '13 at 08:27
  • I editted #yolo to %23yolo which is the correct url. Yes, the url was working fine until the recent twitter update to v1.1. The previous working version: [link](https://dev.twitter.com/docs/api/1/get/search) Current v1.1: [link](https://dev.twitter.com/docs/api/1.1/get/search/tweets) The difference I can made from those two are the url change to `'https://dev.twitter.com/docs/api/1.1/get/search/tweets.json?q=%23yolo&count=10&result_type=recent&callback=?'` and I attempt to change json.results to json.statuses but still failed. – Edeson Lee Yan Wei Jun 20 '13 at 08:43

1 Answers1

19

The reason it's not working

In the wonderful world of bad ideas, Twitter is sunsetting this answer, as of May 2013, and will require, at minimum, that you either use one of their widgets, and shoehorn it in, or that you set up an application and do application-level authentication, even for public-timeline GET requests. [From this post]

This is entirely true. You either 'hack' together something using their wigets (highly discouraged, and if they change one line of code in their widget, your code will stop working completely), or you do what they suggest and upgrade to authenticated requests using OAuth and the 1.1 API.

Links you must read

So, you can't just change the /1/ to /1.1/ in the URL and expect it to work.

This post explains how the 1.0 API is deprecated, provides evidence from the twitter site, and explains how you need to perform authenticated requests.

This post has a little information on the 1.1 API and how it returns JSON formatted data.

This post explains how you'll get a 410 GONE status if you try and make any requests to the 1.0 API from now on, and what that means.

This post explains what the error you're getting means

...and finally, This post explains step-by-step how, if you choose to use as your server-side language, you make authenticated requests and requires a simple library (single file include) to get it to work.

In Closing

Don't hack something together using JavaScript, as soon as Twitter makes an update to their widget, that's it, you're screwed. Use a server-side language and do it properly as per their documentation.

Community
  • 1
  • 1
Jimbo
  • 25,790
  • 15
  • 86
  • 131
  • Thank you sir, I'll go and read them. Didn't know I was hacking and not using the correct way to get tweets in the first place since I got the codes from this website. – Edeson Lee Yan Wei Jun 20 '13 at 09:05
  • I don't think Jimbo was referring to your old code as hacking, but rather that you don't try to get API 1.1 working in pure Javascript using "hacks". – Strille Jun 20 '13 at 09:41
  • Yes, sorry if it wasn't clear. Any attempts at a JavaScript only solution are 'hacky' and prone to error (and they're also much harder to work with). – Jimbo Jun 20 '13 at 09:42
  • 1
    Thank you so much. I've been looking everywhere for a summary like this – NcAdams Apr 30 '14 at 12:40