first working oAuth Twittia

This commit is contained in:
Jeena Paradies 2010-05-02 17:37:14 +02:00
parent bba00dd94a
commit 301fa12b21
33 changed files with 1260 additions and 46 deletions

View file

@ -1,10 +1,8 @@
function Twittia(action, oauth_key, oauth_secret) {
function Twittia(action) {
this.max_length = 100;
this.since_id;
this.timeout = 2 * 60 * 1000;
this.action = action;
this.oauth_key = oauth_key;
this.oauth_secret = oauth_secret;
this.getNewData();
this.unread_mentions = 0;
@ -170,35 +168,66 @@ Twittia.prototype.getTemplate = function() {
return jQuery.extend(true, {}, this.template);
}
Twittia.prototype.getNewData = function(supress_new_with_timeout) {
var url = "http://api.twitter.com/1/statuses/" + this.action + ".json"
url += "?count=" + this.max_length;
if(this.since_id) url += "&since_id=" + this.since_id;
var parameters = {count: this.max_length}
if(this.since_id) parameters.since_id = this.since_id
var url2 = "?count=" + this.max_length;
if(this.since_id) url2 += "&since_id=" + this.since_id;
var _this = this;
var message = { method:"GET" , action:url, parameters: parameters };
OAuth.completeRequest(message,
{ consumerKey : controller.oauth.consumerToken.key
, consumerSecret: controller.oauth.consumerToken.secret
, token : controller.oauth.accessToken.key
, tokenSecret : controller.oauth.accessToken.secret
});
$.ajax({
url: url,
dataType: 'json',
success: function(data) {
_this.newStatus(data, supress_new_with_timeout);
},
error:function (xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
setTimeout(function() { _this.getNewData(supress_new_with_timeout) }, this.timeout);
}
$.ajax(
{ beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", OAuth.getAuthorizationHeader("", message.parameters));
},
url: url + url2,
dataType: 'json',
success: function(data) {
_this.newStatus(data, supress_new_with_timeout);
},
error:function (xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
setTimeout(function() { _this.getNewData(supress_new_with_timeout) }, this.timeout);
}
});
}
Twittia.prototype.sendNewTweet = function(tweet, in_reply_to_status_id) {
var url = "http://api.twitter.com/1/statuses/update.json";
var _this = this;
var data = "source=twittia&status=" + tweet;
if(in_reply_to_status_id != '') data += "&in_reply_to_status_id=" + in_reply_to_status_id
var parameters = { source: "twittia", status: tweet };
if(in_reply_to_status_id != '') parameters.in_reply_to_status_id = in_reply_to_status_id
var _this = this;
var message = { method:"POST" , action:url, parameters: parameters };
OAuth.completeRequest(message,
{ consumerKey : controller.oauth.consumerToken.key
, consumerSecret: controller.oauth.consumerToken.secret
, token : controller.oauth.accessToken.key
, tokenSecret : controller.oauth.accessToken.secret
});
$.ajax({
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", OAuth.getAuthorizationHeader("", message.parameters));
},
url: url,
type: 'POST',
data: data,
@ -231,6 +260,29 @@ Twittia.prototype.retweet = function(status_id, item) {
});
}
Twittia.prototype.authorizationHeader = function(method, url, params) {
if(params == undefined)
params = '';
if(method == undefined)
method = 'GET';
var timestamp = OAuth.timestamp();
var nonce = OAuth.nonce(11);
var accessor = { consumerSecret: controller.oauth.consumerToken.secret, tokenSecret: controller.oauth.accessToken.secret };
var message = {method: method, action: url, parameters: OAuth.decodeForm(params)};
message.parameters.push(['oauth_consumer_key',controller.oauth.consumerToken.key]);
message.parameters.push(['oauth_nonce',nonce]);
message.parameters.push(['oauth_signature_method','HMAC-SHA1']);
message.parameters.push(['oauth_timestamp',timestamp]);
message.parameters.push(['oauth_token', controller.oauth.accessToken.key]);
message.parameters.push(['oauth_version','1.0']);
message.parameters.sort()
OAuth.SignatureMethod.sign(message, accessor);
return OAuth.getAuthorizationHeader("", message.parameters);
}
function replaceURLWithHTMLLinks(text) {
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
return text.replace(exp,"<a href='$1'>$1</a>");
@ -254,4 +306,5 @@ function loadPlugin(url) {
document.getElementsByTagName("head")[0].appendChild(plugin);
}
var twittia_instance;