1

I used jquery-ajax method POST to post authorization headers, but Firebug show an error "401 Unauthorized" headers as parameters of the method.

What am I doing wrong? And what should I do?

jQuery(function(dat) {
dat.ajax({
  type: "POST",
  url: url,
  data: {
    Latlng: 'coordinates.coordinates',
    texts: 'text'
  },
  success: function(dat) {
    setMarkers(dat);
  },
  dataType: "json"
});
});

And this is oAuth method I forgot (I don't think here is something wrong because I made it as in example.)

var url = "https://stream.twitter.com/1/statuses/filter.json"; var accessor = { token: "token", tokenSecret: "token_secret", consumerKey : "consumer_key", consumerSecret: "consumer_secret" };

var message = {
  action: url,
  method: "POST",
  parameters: {
    track: 'nutella',
    locations: '-180,-90,180,90'
  }
};

OAuth.completeRequest(message, accessor);
OAuth.SignatureMethod.sign(message, accessor); url = url + '?' + OAuth.formEncode(message.parameters);

I closed secret data.

user1499804
  • 227
  • 2
  • 6
  • 11

1 Answers1

6

You have to send an additional header. I know if you are trying to call google api they ask for a header titled Authorization. Here is how you would do that via jQuery.

function setHeader(xhr) {

 xhr.setRequestHeader('Authorization', '12345');
 xhr.setRequestHeader('SomethingElse', 'abcdefg');

}

$.ajax({

        url: 'www.google.com',
    type: 'POST',
    datatype: 'json',
    success: function() { alert("Success"); },
    error: function() { alert('Failure!'); },
    beforeSend: setHeader

});
Chris
  • 1,539
  • 13
  • 25
  • But Twitter Streaming API suggests oAuth. And I am trying authorize exactly this. – user1499804 Jul 23 '12 at 22:54
  • Whatever header you need to send that's how you would do it. XMLHttpRequest (this is what you are sending) sets headers in this way. Just a Key value pair. – Chris Jul 23 '12 at 23:02
  • Twitter does have samples so just see what they are doing there. – Chris Jul 23 '12 at 23:05