slowydown
slowydown

Reputation: 191

Twitter API setting Authorization header in jQuery AJAX

I have a problem. I have the Authorization header generated by OAuth Tool in Twitter (I hid value of keys)

Authorization: OAuth oauth_consumer_key="xxxxxxxxxxxxxxxx", oauth_nonce="xxxxxxxxxxxxxxxx", oauth_signature="xxxxxxxxxxxxxxx", oauth_signature_method="HMAC-SHA1", oauth_timestamp="xxxxxxxxx", oauth_version="1.0"

And I have a AJAX code:

$.ajax({
    url: 'https://api.twitter.com/1.1/search/tweets.json?q=%23freebandnames&since_id=24012619984051000&max_id=250126199840518145&result_type=mixed&count=4',
    type: 'GET',
    beforeSend: function (xhr) {
            xhr.setRequestHeader('Authorization', 'OAuth oauth_consumer_key="xxxxxxxxxxxxxxx", oauth_nonce="xxxxxxxxxxxxxxx", oauth_signature="xxxxxxxxxxxxxxx", oauth_signature_method="HMAC-SHA1", oauth_timestamp="xxxxxxxxx", oauth_version="1.0"');
        },
    dataType: "jsonp",
    success: function(data) {
        console.log("Success: " + data);
        }
    },
    error: function(data) {
        console.log("Error " + data);
    }

});

But this code returned error

Object {readyState: 4, status: 404, statusText: "error"}

Here is part of documentation Twitter REST API which I use: https://dev.twitter.com/rest/reference/get/search/tweets

What is wrong? Any ideas?

Upvotes: 0

Views: 1870

Answers (1)

Jonas
Jonas

Reputation: 4048

Twitter does not support generating the authentication header from a webpage. You must go through the "handshake" using their site to authenticate and redirecting back to your webpage.

If you want to avoid this and keep your oauth keys locally, you will need to build a server which can authenticate using stored keys. Then your webpage will make Twitter requests via your server.

Upvotes: 1

Related Questions