-
Notifications
You must be signed in to change notification settings - Fork 18
/
dropbox.js
100 lines (82 loc) · 2.31 KB
/
dropbox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var dropbox = {
setup: function(consumerKey, consumerSecret) {
this._consumerKey = consumerKey;
this._consumerSecret = consumerSecret;
this._requestCounter = $.now();
},
authenticate: function(email, password, callback) {
var that = this;
this._request("/token", {
sendAuth: false,
success: function(data) {
console.log("authentication response", data);
that._accessToken = data.token;
that._accessTokenSecret = data.secret;
if (callback) {
callback();
}
},
error: function() {
console.error("authentication error", arguments);
}
}, {
email: email,
password: password
});
},
getInfo: function() {
this._request("/account/info", {
success: function(data) {
console.log("account info", data);
},
error: function() {
console.log("account info error", arguments);
}
});
},
_request: function(path, params, data) {
var requestId = "dropboxjsonp" + (this._requestCounter++);
params = $.extend({}, {
subdomain: "api", // some methods need api-content.dropbox.com
apiVersion: "0",
sendAuth: true,
method: "GET",
success: $.noop,
error: $.noop
}, params || {});
if (params.sendAuth && !this._accessToken) {
throw "Authenticated method called before authenticating";
}
var url = "https://" + params.subdomain + ".dropbox.com/" + params.apiVersion + path;
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);
$.ajax({
dataType: "jsonp",
method: params.method,
url: url,
data: OAuth.getParameterMap(message.parameters),
jsonpCallback: requestId,
success: params.success,
error: params.error
});
}
};