0

I tried to use Twitter API to post a tweet using Javascript. Details Below

Base String

POST&http%3A%2F%2Fapi.twitter.com%2F1%2Fstatuses%2Fupdate.json&oauth_consumer_key%3DXXXXXXXXXXX%26oauth_nonce%3D9acc2f75c97622d1d2b4c4fb4124632b1273b0e0%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1305227053%26oauth_token%3D159970118-XXXXXXXXXXXXXXXXXXXXXXX%26oauth_version%3D1.0%26status%3DHello

Header

OAuth oauth_nonce="9acc2f75c97622d1d2b4c4fb4124632b1273b0e0", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1305227053", oauth_consumer_key="XXXXXXXXXXXXXXXXX", oauth_token="159970118-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", oauth_signature="IWuyoPJBrfY03Hg5QJhDRtPoaDs%3D", oauth_version="1.0"

I used POST method with body "status=Hello"

But i get a INTERNAL SERVER ERROR.. IS there any mistake on my side ?? Thanks in advance.

Javascript code used

h is the header given above

tweet="Hello"

encodeURLall is user defined which is working in all other occasions.

var xhr = new XMLHttpRequest();
xhr.open("POST","http://api.twitter.com/1/statuses/update.json", false);
xhr.setRequestHeader("Authorization",h);

xhr.onreadystatechange = function() {

    if (xhr.readyState == 4 )
    {
        console.log("STATUS="+xhr.status);  
        console.log("RESPONSE="+xhr.responseText);  
    }
}

xhr.send("status="+encodeURLall(tweet));

}
Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
Ram
  • 91
  • 1
  • 1
  • 6

1 Answers1

0

You cannot access Twitter's site using an XMLHttpRequest, due to Same origin policy. Use JSONP instead or a server-side proxy (call your own server that redirects your request to Twitter).

BTW, what does encodeURLall() do? Shouldn't you just use encodeURIComponent?


Update: To quote Google:

Regular web pages can use the XMLHttpRequest object to send and receive data from remote servers, but they're limited by the same origin policy. Extensions aren't so limited. An extension can talk to remote servers outside of its origin, as long as it first requests cross-origin permissions.

Please read on there to see which settings you should change in order to make this work.

Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
  • So i should be doing like this `$.ajax({ 'url': 'http://api.twitter.com/1/statuses/update.json', 'data': { status: encodeURLall(tweet) }, 'type': 'post', 'success': onUpdateCallback, 'dataType': 'jsonp' });` but how would i set header for the request. – Ram May 14 '11 at 12:10
  • Btw i am trying to create a Google Chrome Extension. Just wanna give you clear idea on wat i am doing. So is it not possible to set headers in jsonp ??? – Ram May 14 '11 at 12:19
  • @Ram: In a Chrome extension it might be different. – Marcel Korpel May 14 '11 at 12:55
  • @MarcelKorpel So you mean XMLHttp Request is possible or JSONP with custom headers in chrome extension? – Ram May 14 '11 at 12:58
  • @Ram: According to the [link to Google's own description](http://code.google.com/chrome/extensions/xhr.html) in the dupe, it is. – Marcel Korpel May 14 '11 at 13:24
  • @MarcelKorpel I already have given permissions for twitter as told in the google site. so i think i should ask Twitter about this. Anyways Thank you. – Ram May 14 '11 at 13:50