0

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?

slowydown
  • 191
  • 1
  • 2
  • 11

1 Answers1

1

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.

Jonas
  • 3,969
  • 2
  • 25
  • 30
  • Shouldn't you be able to use a bearer token and application only authorization? – Ketema Feb 07 '17 at 16:54
  • I think that's possible -- use AJAX to send a POST command with your base64-encoded key and secret; get the access code from the returned data, and you are on your way. Of course Twitter would hate you because you are exposing your credentials to the world :) – Jonas Feb 07 '17 at 20:34