Shiv Deepak
Shiv Deepak

Reputation: 3116

Update twitter status without prompting user using pure JavaScript

I have web application where people can login from twitter and based on their preference and DOM events I need update their status on twitter. I have a good idea how to do this on server side, but for this project I am not using any server side code, So how can I do this by just javascript, @anywhere twitter api and twitter intents are taking me to no where because they prompt user for submitting the tweet which I dont want.

Upvotes: 1

Views: 1294

Answers (2)

newbie
newbie

Reputation: 1505

A pure Javascript solution to consume twitter API is not possible without compromising your consumer secret key. Twitter API authenticates every request using a HMAC-SHA1 token, the SHA1 token is generated using the public/private key assigned by twitter to your api account. If you plan to generate this token using a pure javascript SHA1 implementation then it means you will be exposing your private key in the javascript code which anyone can look at.

Even though technically its possible (provided you can find a javascript library which implements SHA1), its not advisable.

FYI, jQuery.Ajax method does let you modify the headers of the ajax request by tapping in to the beforeSend(jqXHR, settings) method.

Upvotes: 3

shanethehat
shanethehat

Reputation: 15560

You should be able to do this with an AJAX POST request to the REST API. Documentation: https://dev.twitter.com/docs/api/1/post/statuses/update

You target the URL http://api.twitter.com/1/statuses/update.format, where format can be either xml or json, and reflects the format of the response. Required data is the status text, and there are several optional parameters which I won't list here. This only works for the currently authenticated user.

An (untested) example using jQuery:

var message = "This is a tweet, there are many like it but this one is mine";

$.ajax({
    type: "POST",
    url: "http://api.twitter.com/1/statuses/update.json",
    data: "status="+message,
    datatype: "json"
});

Upvotes: 0

Related Questions