0
votes

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));

}
1
Are you POSTing from the browser? or is there server-side code doing the post?jimbo
Can you show us the code that you use?Marcel Korpel
@jimbojw i am posting from the browser only.Ram
@MarcelKorpel Have edited the post above with code.Ram

1 Answers

0
votes

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.