This repository has been archived by the owner on Feb 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
oauth_request.js
82 lines (64 loc) · 2.27 KB
/
oauth_request.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
var OAuthRequest = function(config) {
this.apiRoot = config.apiRoot;
this.urlConf = config.urlConf;
this.consumer = {
consumerKey : config.consumerKey,
consumerSecret : config.consumerSecret
};
this.oauth = OAuth(this.consumer);
this.requestParams = "";
};
OAuthRequest.prototype.setAccessTokens = function (oauthToken, oauthTokenSecret) {
this.oauth.setAccessToken([oauthToken, oauthTokenSecret]);
};
OAuthRequest.prototype.requestAuth = function() {
var fail = function(data) { console.error('fail'); console.dir(data);};
this.oauth.get(this.urlConf.root+this.urlConf.request_token, function(data){
if(data && data.text) {
Platform.openURL(this.urlConf.root +this.urlConf.authorize + "?"+ data.text);
this.requestParams = data.text;
} else {
fail(data);
}
}.bind(this),fail);
};
// get the oauth_token and oauth_token_secret from the response
OAuthRequest.prototype.secretAndToken = function (str) {
var r = {};
str.split("&").forEach(function(data){
var pairs = data.split("=");
r[pairs[0]] = pairs[1];
});
return r;
};
OAuthRequest.prototype.userAuthorize = function (pin, callbacks) {
var fail = function(data) { console.error('fail'); console.dir(data); callbacks.onFailure();};
this.oauth.get(this.urlConf.root +this.urlConf.access_token + "?oauth_verifier="+pin+"&"+this.requestParams,
function(data){
var importantStuff = this.secretAndToken(data.text);
this.setAccessTokens(importantStuff.oauth_token, importantStuff.oauth_token_secret);
callbacks.onSuccess(importantStuff);
}.bind(this),fail);
};
OAuthRequest.prototype.get = function(url, callbacks) {
this.oauth.get(url, callbacks.onSuccess, callbacks.onFailure);
};
OAuthRequest.prototype.post = function(url, data, callbacks) {
this.oauth.post(url,data, callbacks.onSuccess, callbacks.onFailure);
};
OAuthRequest.prototype['delete'] = function(url, callbacks) {
this.oauth.request( {
method : 'DELETE',
url : url,
success: callbacks.onSuccess,
failure: callbacks.onFailure
});
};
OAuthRequest.prototype.put = function(url, callbacks) {
this.oauth.request({
method : 'PUT',
url : url,
success: callbacks.onSuccess,
failure: callbacks.onFailure
});
};