-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdropbox.lua
371 lines (295 loc) · 11 KB
/
dropbox.lua
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
-- Copyright Michael Weingarden 2013. All rights reserved.
--main works well is last known good build
--most of this was constructed based on the advice found here:
--https://www.dropbox.com/developers/blog/20 and here:
--https://www.dropbox.com/developers/core/api#request-token
local widget = require( "widget" )
local lfs = require "lfs"
consumer_key = "" -- key string goes here
consumer_secret = "" -- secret string goes here
webURL = "http://www.google.com"
myFile = "2ndPeriod.csv"
mySigMethod = "PLAINTEXT"
access_token = ""
access_token_secret = ""
request_token = ""
request_token_secret = ""
accountInfo = ""
_W = display.contentWidth
_H = display.contentHeight
local myText = display.newText(accountInfo, 0, 5*_H/8 - 100, 400, 600, native.systemFont, 16)
myText:setTextColor(255, 255, 255)
local function loadToken( type )
local saveData = ""
local path = system.pathForFile( type..".txt", system.DocumentsDirectory )
local file = io.open( path, "r" )
if file then
--print("Found textField file")
saveData = file:read( "*a" )
io.close( file )
end
file = nil
return saveData
end
local function storeToken( type, data )
local path = system.pathForFile( type..".txt", system.DocumentsDirectory )
local file = io.open( path, "w" )
file:write( data )
io.close( file )
file = nil
end
local function rawGetRequest(url, rawdata)
local function rawGetListener( event )
if event.isError then
print( "Network error!", event.status, event.response)
else
print ( "rawGetListener RESPONSE: ", event.status, event.response ) -- **debug
myText.text = "Data received. Press Display Info to view."
end
-- the event.response is the requested data from Dropbox
-- you can either process the response here or use a global variable or pass it to
-- another function
-- using accountInfo to either store account info or incoming text file
accountInfo = event.response
end
url = url.."?"..rawdata
local result = network.request( url, "GET", rawGetListener)
return result
end
local function rawPostRequest(url, rawdata, callback)
local function rawPostListener( event )
if event.isError then
print( "Network error!", event.status, event.response)
else
print ( "Dropbox RESPONSE: ", event.status, event.response ) -- **debug
end
if callback then
print("calling back from rawPostRequest")
callback( event.isError, event.response) -- return with response
end
end
local params = {}
local headers = {}
print("rawdata "..rawdata)
headers["Content-Type"] = "text/plain"
headers["Authorization"] = "OAuth "..rawdata
params.headers = headers
local result = network.request( url, "POST", rawPostListener, params)
return result
end
local function getRequestToken( consumer_key, token_ready_url, request_token_url,
consumer_secret, callback )
--Your HTTP request should have the following header:
--Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="<app-key>", oauth_signature="<app-secret>&"
local post_data = "oauth_version=\"1.0\", oauth_signature_method=\""..mySigMethod.."\", oauth_consumer_key=\""
..consumer_key.."\", oauth_signature=\""..consumer_secret.."&\""
return rawPostRequest(request_token_url, post_data, callback)
end
local function getAccessToken(token, token_secret, consumer_key, consumer_secret,
access_token_url, callback)
--Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="<app-key>", oauth_token="<request-token>", oauth_signature="<app-secret>&<request-token-secret>"
local post_data = "oauth_version=\"1.0\", oauth_signature_method=\""..mySigMethod.."\", oauth_consumer_key=\""..consumer_key.."\", oauth_token=\""..token.."\", oauth_signature=\""..consumer_secret.."&"..token_secret.."\""
return rawPostRequest(access_token_url, post_data, callback)
end
local function responseToTable(str, delimiters)
local obj = {}
while str:find(delimiters[1]) ~= nil do
if #delimiters > 1 then
local key_index = 1
local val_index = str:find(delimiters[1])
local key = str:sub(key_index, val_index - 1)
str = str:sub((val_index + delimiters[1]:len()))
local end_index
local value
if str:find(delimiters[2]) == nil then
end_index = str:len()
value = str
else
end_index = str:find(delimiters[2])
value = str:sub(1, (end_index - 1))
str = str:sub((end_index + delimiters[2]:len()), str:len())
end
obj[key] = value
--print(key .. ":" .. value) -- **debug
else
local val_index = str:find(delimiters[1])
str = str:sub((val_index + delimiters[1]:len()))
local end_index
local value
if str:find(delimiters[1]) == nil then
end_index = str:len()
value = str
else
end_index = str:find(delimiters[1])
value = str:sub(1, (end_index - 1))
str = str:sub(end_index, str:len())
end
obj[#obj + 1] = value
end
end
return obj
end
local function authorizeDropbox(event)
local remain_open = true
print("event.url: "..event.url)
print("webURL: "..webURL)
print("authorizeDropbox: ", event.url)
local callbackURL = true
local url = event.url
if url:find("callback") then
callbackURL = true
else
callbackURL = false
end
if url:find("oauth_token") and not callbackURL then
remain_open = false
function getAccess_ret( status, access_response )
print("getAccess_ret")
print("access_response: "..access_response)
access_response = responseToTable( access_response, {"=", "&"} )
access_token = access_response.oauth_token
access_token_secret = access_response.oauth_token_secret
user_id = access_response.user_id
screen_name = access_response.screen_name
storeToken( "access_token", access_token )
storeToken( "access_token_secret", access_token_secret )
end
print("getAccess")
getAccessToken(request_token, request_token_secret, consumer_key,
consumer_secret, "https://api.dropbox.com/1/oauth/access_token", getAccess_ret )
end
return remain_open
end
local function requestToken_ret( status, result )
print("requestToken_ret")
print("result: "..result)
request_token = result:match('oauth_token=([^&]+)')
request_token_secret = result:match('oauth_token_secret=([^&]+)')
print("request_token_secret: "..request_token_secret)
-- Displays a webpopup to access the Twitter site so user can sign in
-- urlRequest dictates whether the WebPopup will remain open or not
native.showWebPopup(0, 0, 320, 480, "https://www.dropbox.com/1/oauth/authorize?oauth_token="
.. request_token.."&oauth_callback="..webURL, {urlRequest = authorizeDropbox})
end
local function connect( event )
local dropbox_request = (getRequestToken(consumer_key, webURL,
"https://api.dropbox.com/1/oauth/request_token", consumer_secret, requestToken_ret))
end
local function getSomething( event )
print("pre get info request")
--Your HTTP request should have the following form:
--Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="<app-key>",
--oauth_token="<access-token>, oauth_signature="<app-secret>&<access-token-secret>"
--formatted for GET
local post_data = "oauth_version=1.0&oauth_signature_method="..mySigMethod.."&oauth_consumer_key="..consumer_key.."&oauth_token="..access_token.."&oauth_signature="..consumer_secret.."%26"..access_token_secret
--formatted for POST which doesn't seem to work for file requests
--local post_data = "oauth_version=\"1.0\", oauth_signature_method=\""..mySigMethod.."\", oauth_consumer_key=\""..consumer_key.."\", oauth_token=\""..access_token.."\", oauth_signature=\""..consumer_secret.."&"..access_token_secret.."\""
--use this url if you just want account info
--local url = "https://api.dropbox.com/1/account/info"
--use this url if you want to download a plain text file
local url = "https://api-content.dropbox.com/1/files/dropbox/Public/"..myFile
print("post get info request")
local result1 = rawGetRequest(url, post_data)
print("rawGetRequest result: "..tostring(result1))
end
local function displayInfo()
print("accountInfo: "..accountInfo)
myText.text = accountInfo
local path = system.pathForFile( myFile, system.DocumentsDirectory )
local file = io.open( path, "w" )
file:write( accountInfo )
io.close( file )
file = nil
end
local function putFileListener( event )
print("putFileListener")
if event.isError then
print( "Network error!", event.status, event.response)
else
print ( "Dropbox RESPONSE: ", event.status, event.response ) -- **debug
end
end
local function putFile()
--formatted for POST which doesn't seem to work for file requests
local post_headers = "oauth_version=\"1.0\", oauth_signature_method=\""..mySigMethod.."\", oauth_consumer_key=\""..consumer_key.."\", oauth_token=\""..access_token.."\", oauth_signature=\""..consumer_secret.."&"..access_token_secret.."\""
--Note: Providing a Content-Length header set to the size of the uploaded file is required so that the server can verify that it has received the entire file contents.
local path = system.pathForFile( myFile, system.DocumentsDirectory )
local filesize = lfs.attributes (path, "size")
--use this url if you want to upload a file file
local url = "https://api-content.dropbox.com/1/files_put/dropbox/Public/testFromapp.txt"
local params = {}
local headers = {}
headers["Content-Type"] = "text/plain"
headers["Content-Length"] = fileSize
headers["Authorization"] = "OAuth "..post_headers
params.headers = headers
-- This tells network.request() to get the request body from a file...
params.body = {
filename = myFile,
baseDirectory = system.DocumentsDirectory
}
print("rawPostRequest posting")
local result = network.request( url, "POST", putFileListener, params)
end
access_token = loadToken( "access_token" )
access_token_secret = loadToken( "access_token_secret")
-- there is no need to show the Connect button if the user has already
-- authorized Dropbox access previously
if access_token == "" then
connectButton = widget.newButton
{
left = 380,
top = _H/8 - 100,
width = 200,
height = 50,
id = "button1",
defaultFile = "smallButton.png",
overFile = "smallButtonOver.png",
label = "Connect",
fontSize = 34,
onRelease = connect
}
connectButton.x = _W / 2
end
getInfoButton = widget.newButton
{
left = 380,
top = 2*_H/8 - 100,
width = 200,
height = 50,
id = "button3",
defaultFile = "smallButton.png",
overFile = "smallButtonOver.png",
label = "Get Info",
fontSize = 34,
onRelease = getSomething
}
getInfoButton.x = display.contentWidth / 2
displayInfoButton = widget.newButton
{
left = 380,
top = 3*_H/8 - 100,
width = 200,
height = 50,
id = "button3",
defaultFile = "smallButton.png",
overFile = "smallButtonOver.png",
label = "Display Info",
fontSize = 34,
onRelease = displayInfo
}
displayInfoButton.x = display.contentWidth / 2
putBtn = widget.newButton
{
left = 380,
top = 4*_H/8 - 100,
width = 200,
height = 50,
id = "button4",
defaultFile = "smallButton.png",
overFile = "smallButtonOver.png",
label = "Put File",
fontSize = 34,
onRelease = putFile
}
putBtn.x = display.contentWidth / 2