-
Notifications
You must be signed in to change notification settings - Fork 2
/
instagram.js
172 lines (140 loc) · 4.24 KB
/
instagram.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Constructor
exports.Instagram = function(options) {
/* Here we have the parameters to access the API.
* Note that we try to load it from system parameters first
*/
if(!options) {
apiData = {
client_id : '999999999999999999',
client_secret : '99999999999999999999',
authorize_url : 'https://instagram.com/oauth/authorize/',
request_url : 'https://api.instagram.com/v1/',
redirect_uri : 'http://www.xxxxx.com',
response_type : 'token',
scope : '',
display : 'touch',
access_token : Ti.App.Properties.getString('instagramAccessToken')
}
}
// Interface Attributes
var window = null;
var winBase = null;
var view = null;
var webView = null;
}
/*
* Private functions
*/
function saveToken(token) {
Ti.App.Properties.setString('instagramAccessToken', token);
apiData.access_token = token;
}
function hide() {
if(!window) {
return;
}
try {
winBase.close();
window = null;
winBase = null;
} catch(ex) {
Ti.API.debug('Cannot destroy the authorize UI. Ignoring.');
}
}
function createAuthWindow(callback) {
closeButton = Ti.UI.createButton({
title : 'Cancel',
style : Ti.UI.iPhone.SystemButtonStyle.DONE
});
winBase = Ti.UI.createWindow({
modal : true,
navBarHidden : true
});
window = Ti.UI.createWindow({
leftNavButton : closeButton
});
var navigationGroupTwitter = Ti.UI.iPhone.createNavigationGroup({
window : window
});
winBase.add(navigationGroupTwitter);
view = Ti.UI.createView({
backgroundColor : 'white'
});
webView = Ti.UI.createWebView({
url : apiData.authorize_url + '?client_id=' + apiData.client_id + '&redirect_uri=' + apiData.redirect_uri + '&response_type=token&display=touch',
autoDetect : [Ti.UI.iOS.AUTODETECT_NONE]
});
webView.addEventListener('load', function(e) {
Ti.API.info(e);
if (e.url.indexOf('access_token=') != -1) {
webView.stopLoading();
saveToken(e.url.substring(e.url.indexOf('access_token=') + 13));
hide();
if (callback) {
callback();
}
}
});
view.add(webView);
closeButton.addEventListener('click', function(e) {
Ti.App.fireEvent('app:instagram_canceled');
hide();
});
window.add(view);
winBase.open({
modal : true,
modalStyle : Ti.UI.iPhone.MODAL_PRESENTATION_FULLSCREEN
});
}
/*
* Public Functions
*/
// request the user to authorize Instagram account
exports.Instagram.prototype.authorize = function(callback) {
if(!apiData.access_token) {
createAuthWindow(callback);
if(apiData.access_token) {
Ti.App.fireEvent('app:instagram_integrated');
} else {
Ti.API.debug('Failed to authorize Instagram account.');
Ti.App.fireEvent('app:instagram_failed');
}
}
}
// Perform a request to the API
exports.Instagram.prototype.request = function(url, method, parameters, callback) {
var urlToSend = apiData.request_url + url + '?access_token=' + apiData.access_token;
// adding the paramenters to the url
var strParam = '';
if(method === 'GET') {
for(var key in parameters) {
if(parameters.hasOwnProperty(key)) {
var obj = parameters[key];
for(var prop in obj) {
if(obj.hasOwnProperty(prop)) {
strParam += '&' + prot + '=' + obj[prop];
}
}
}
}
urlToSend += strParam;
}
var remote = Ti.Network.createHTTPClient();
remote.onload = function() {
callback(this.responseText);
}
remote.onError = function() {
Ti.App.fireEvent("app:twitter_error");
callback(null);
}
remote.open(method, urlToSend, true);
if(method === 'POST') {
remote.send(parameters)
} else {
remote.send();
}
}
exports.Instagram.prototype.logout = function() {
apiData.access_token = null;
Ti.App.Properties.removeProperty('instagramAccessToken');
}