最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Need help converting to Twitter API v1.1 - JavaScript - Stack Overflow

programmeradmin1浏览0评论

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 = '.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 .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?

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?

Share Improve this question edited Jul 15, 2013 at 13:28 Jimbo 26.6k16 gold badges92 silver badges133 bronze badges asked Jun 20, 2013 at 7:37 Edeson Lee Yan WeiEdeson Lee Yan Wei 331 gold badge1 silver badge4 bronze badges 4
  • Have you debugged it? On which line is it stopping? – LuigiEdlCarno Commented Jun 20, 2013 at 7: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 Commented Jun 20, 2013 at 8: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 Commented Jun 20, 2013 at 8: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 Current v1.1: link 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 Commented Jun 20, 2013 at 8:43
Add a comment  | 

1 Answer 1

Reset to default 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 php 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.

发布评论

评论列表(0)

  1. 暂无评论