Dmitrii Mikhailov
Dmitrii Mikhailov

Reputation: 5221

405 Error when I'm trying to search in twitter

I'm trying to do some searching in Twitter using jQuery. Unfortunately, It's returning only:

https://api.twitter.com/1.1/search/tweets.json?q=django 405 (Method Not Allowed)
XMLHttpRequest cannot load https://api.twitter.com/1.1/search/tweets.json?q=django.
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

Here's the code:

(function(){
    var Twitter = {
        init: function(){
            this.url = 'https://api.twitter.com/1.1/search/tweets.json?q=django';
            this.fetch();
        },

        fetch: function(){
            $.ajax({
                url: this.url,
                type: 'GET',
                dataType: 'json',
                success: function(data){ console.log(data); },
                error: function(data){ console.log(data); },
                beforeSend: this.setHeader
            })
        },

        setHeader: function(xhr){
            xhr.setRequestHeader('X-HostCommonName', 'api.twitter.com');
            xhr.setRequestHeader('Authorization', 'OAuth my_oauth_data');
            xhr.setRequestHeader('Host', 'api.twitter.com');
            xhr.setRequestHeader('X-Target-URI', 'https://api.twitter.com');
        }
    };

    Twitter.init();
})();

I've tested the same query with the same headers using REST Console extension for Google Chrome and It worked.

Upvotes: 1

Views: 482

Answers (1)

Dmitrii Mikhailov
Dmitrii Mikhailov

Reputation: 5221

Well, It looks like (here's the info) you can't do OAuth authentication on client side.

So, the only solution here is to code this thing to be invoked on your server.

Upvotes: 1

Related Questions