Created
November 22, 2011 06:17
-
-
Save dusual/1385035 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function escapePath(p) { | |
return encodeURIComponent(p) | |
.replace(/%2F/g, '/') | |
.replace(/\)/g, '%29') | |
.replace(/\(/g, '%28'); | |
} | |
// test for jsonp callback functionality | |
function dropboxjsonprandom_callback(data){ | |
alert(data); | |
} | |
var dropbox = { | |
setup: function() { | |
this._consumerKey = 'someKey'; | |
this._consumerSecret = 'someSecret'; | |
this._requestCounter = $.now(); | |
}, | |
authenticate_token: function(callback) { | |
var that = this; | |
this._request("/oauth/request_token", { | |
sendAuth: false, | |
success: function(data) { | |
console.log("authentication response", data); | |
var temp_list = data.split('&'); | |
that._requestToken = temp_list[1].split('=')[1]; | |
that._requestTokenSecret = temp_list[0].split('=')[1]; | |
if (callback) { | |
callback(); | |
} | |
}, | |
error: function() { | |
console.error("authentication error", arguments); | |
} | |
}, { | |
// email: email, | |
// password: password | |
}); | |
}, | |
authenticate_authorize: function(callback) { | |
return "https://www.dropbox.com/0/oauth/authorize?oauth_token="+dropbox._requestToken; | |
}, | |
authenticate_access_token: function(callback) { | |
var that = this; | |
console.log(this._accessToken); | |
this._request("/oauth/access_token", { | |
sendAuth:false , | |
success: function(data) { | |
console.log("authentication response", data); | |
var temp_list = data.split('&'); | |
that._accessToken = temp_list[1].split('=')[1]; | |
that._accessTokenSecret = temp_list[0].split('=')[1]; | |
if (callback) { | |
callback(); | |
} | |
}, | |
error: function() { | |
console.error("Access token error", arguments); | |
} | |
}, { | |
oauth_token:this._requestToken, | |
oauth_token_secre:this._requestTokenSecret | |
}); | |
}, | |
getFiles: function(path,callback) { | |
this._request("/files/dropbox/"+path, {subdomain:'api-content', | |
success: function(data) { | |
console.log("Getting the file ", data); | |
if (callback) { | |
callback(data); | |
} | |
}, | |
error: function() { | |
console.log("account info error", arguments); | |
} | |
}); | |
}, | |
copyFiles : function(from,to,callback) { | |
this._request("/fileops/copy" ,{ | |
success: function(data) { | |
console.log("Copying the files ", data); | |
if (callback) { | |
callback(data); | |
} | |
}, | |
error: function() { | |
console.log("Error in copying files" , arguments); | |
} | |
}, | |
{from_path:from, | |
to_path:to, | |
root : 'dropbox' | |
}); | |
}, | |
deleteFiles : function(path,callback) { | |
this._request("/fileops/delete" ,{ | |
success: function(data) { | |
console.log("Copying the files ", data); | |
if (callback) { | |
callback(data); | |
} | |
}, | |
error: function() { | |
console.log("Error in copying files" , arguments); | |
} | |
}, | |
{path:path, | |
root : 'dropbox' | |
}); | |
}, | |
createFolder : function(path,callback) { | |
this._request("/fileops/create_folder" ,{ | |
success: function(data) { | |
console.log("Create folder at given path ", data); | |
if (callback) { | |
callback(data); | |
} | |
}, | |
error: function() { | |
console.log("Error in creating folder" , arguments); | |
} | |
}, | |
{path:path, | |
root : 'dropbox' | |
}); | |
}, | |
moveFiles : function(from,to,callback) { | |
this._request("/fileops/move" ,{ | |
success: function(data) { | |
console.log("move files from to to ", data); | |
if (callback) { | |
callback(data); | |
} | |
}, | |
error: function() { | |
console.log("Error in moving files" , arguments); | |
} | |
}, | |
{from_path:from, | |
to_path:to, | |
root : 'dropbox' | |
}); | |
}, | |
putFiles: function(file,callback) { | |
//$.ajaxSetup({ | |
// data: {file:file} | |
// }); | |
this._request("/files/dropbox", {method:"POST",file:file.fileName,extra:file,subdomain:'api-content'}, { | |
success: function() { | |
console.log("post info" ); | |
if (callback) { | |
callback(); | |
} | |
}, | |
error: function() { | |
alert("failure"); | |
} | |
}); | |
}, | |
getMetadata: function(path,callback) { console.log(this._accessToken); | |
this._request("/metadata/dropbox/"+path,{ | |
sendAuth:true, | |
file_limit:10000, | |
list:true, | |
success: function(data) { | |
console.log("account info", data); | |
if (callback) { | |
callback(data); | |
} | |
}, | |
error: function() { | |
console.log("Meta data error", arguments); | |
} | |
}); | |
}, | |
_request: function(path, params, data) { | |
var requestId = "dropboxjsonp" + (this._requestCounter++); | |
/* Mainly for testing how the concept of jsonp callback works */ | |
//var requestId = "dropboxjsonprandom_callback" ; | |
params = $.extend({}, { | |
subdomain: "api", // some methods need api-content.dropbox.com | |
apiVersion: "0", | |
sendAuth: true, | |
method: "GET", | |
success: $.noop, | |
error: $.noop | |
// callback: requestId | |
}, params || {}); | |
// if (params.sendAuth && !this._accessToken) { | |
// throw "Authenticated method called before authenticating"; | |
// } | |
var url = "https://" + params.subdomain + ".dropbox.com/" + params.apiVersion + escapePath(path); | |
console.log(data); | |
var message = { | |
action: url, | |
method: params.method, | |
parameters: { | |
oauth_consumer_key: this._consumerKey, | |
oauth_signature_method: "HMAC-SHA1", | |
callback: requestId | |
} | |
}; | |
$.extend(message.parameters, data); | |
if (params.sendAuth) { | |
message.parameters.oauth_token = this._accessToken; | |
} | |
var oauthBits = { | |
consumerSecret: this._consumerSecret | |
}; | |
if (params.sendAuth) { | |
oauthBits.tokenSecret = this._accessTokenSecret; | |
} | |
OAuth.setTimestampAndNonce(message); | |
OAuth.SignatureMethod.sign(message, oauthBits); | |
/* | |
escaping path after oauth signature has been made | |
*/ | |
var url = "https://" + params.subdomain + ".dropbox.com/" + params.apiVersion + escapePath(path); | |
if (params.subdomain=='api'){ | |
$.ajax({ | |
dataType: "jsonp", | |
method: params.method, | |
url: url, | |
data: OAuth.getParameterMap(message.parameters), | |
jsonpCallback: requestId, | |
success: params.success , | |
error: params.error | |
}); | |
} | |
else{ window.open(url+'?'+$.param(message.parameters)) | |
//window.open(url+'?'+(message.parameters)); | |
} | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment