Need help converting to Twitter API v1.1 - JavaScript

心不动则不痛 提交于 2020-01-10 20:00:08

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/17207850/need-help-converting-to-twitter-api-v1-1-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!