Last active
January 2, 2016 18:09
-
-
Save HyroVitalyProtago/8341278 to your computer and use it in GitHub Desktop.
This file contains 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
-- DropboxAPI | |
-- @Author : Hyro Vitaly Protago | |
-- @Version : 1.0.0 | |
--[[ | |
Need the json lua library (Dkjson) | |
This library is based on : https://www.dropbox.com/developers/core/ | |
Some examples : | |
-- Tools | |
function table.print(tbl, indent) | |
if not indent then indent = 0 end | |
for k, v in pairs(tbl) do | |
local formatting = string.rep(" ", indent) .. k .. ": " | |
if type(v) == "table" then | |
print(formatting) | |
table.print(v, indent+1) | |
else | |
print(formatting .. to string(v)) | |
end | |
end | |
end | |
function showResult(data,status,headers) | |
if data and type(data) == "table" then | |
table.print(data) | |
else | |
print(data) | |
end | |
print(status) | |
table.print(headers) | |
end | |
function setup() | |
-- Create your app in dropbox developer | |
-- (for the examples, app_key and app_secret is visible vars but in real, you need to hide them with save...Data) | |
app_key = "..." | |
app_secret = "..." | |
-- System for Oauth2 identification | |
tween.delay(2, function() | |
DropboxAPI.OAuth2.authorize("code", app_key) | |
end) | |
parameter.text("Code") | |
parameter.text("Submit", function() | |
if Code == "" then return end | |
DropboxAPI.OAuth2.token(Code, app_key, app_secret, function(data, status, headers) | |
-- data is a lua tables like : {"access_token": "ABCDEFG", "token_type": "bearer", "uid": "12345"} | |
-- save the access_token (and the uid) | |
end) | |
-- for use the library, you need just to set DropboxAPI.token with the access token | |
DropboxAPI.token = read...Data("access_token") | |
-- example1 | |
DropboxAPI.files.get("dropbox", "Apps/Codea/img.png", function(data) | |
saveImage("Documents:img", data) | |
end) | |
-- example2 | |
DropboxAPI.metadata("dropbox", "Apps/Codea", showResult) | |
end | |
]]-- | |
DropboxAPI = { | |
locations = { | |
www = "https://www.dropbox.com/", | |
api = "https://api.dropbox.com/", | |
api_content = "https://api-content.dropbox.com/" | |
}, | |
errors = { | |
["400"] = "Bad Input Parameter.", | |
["401"] = "Bad or expired token.", | |
["403"] = "Bad OAuth request.", | |
["404"] = "File or folder not found at the specified path.", | |
["405"] = "Request method not expected.", | |
["429"] = "Your app is making too many requests and is being rate limited.", | |
["500"] = "Dropbox: Internal Server Error ...", | |
["503"] = "If the response includes the Retry-After header, OAuth 1.0 app is being rate limited", | |
["507"] = "User is over Dropbox storage quota." | |
}, | |
token = nil, | |
-- OAuth1 = {}, | |
OAuth2 = {}, | |
files = {}, | |
fileops = {} | |
} | |
--[[ | |
function DropboxAPI.OAuth1.request_token(callback) -- return oauth_token and oauth_token_secret | |
DropboxAPI.http_request("1/oauth/request_token", callback, {method="POST"}) | |
end | |
function DropboxAPI.OAuth1.authorize(oauth_token, oauth_callback) | |
local url = DropboxAPI.locations.www .. "1/oauth/authorize?oauth_token="..oauth_token | |
if oauth_callback then url = url .. "&oauth_callback="..oauth_callback end | |
openURL(url, true) | |
end | |
function DropboxAPI.OAuth1.access_token() -- return oauth_token, oauth_token_secret and uid | |
DropboxAPI.http_request("1/oauth/access_token", callback, {method="POST"}) | |
end | |
]]-- | |
function DropboxAPI.OAuth2.authorize(response_type, client_id) | |
openURL(DropboxAPI.locations.www .. "1/oauth2/authorize?response_type="..response_type.."&client_id="..client_id, true) | |
end | |
function DropboxAPI.OAuth2.token(code, client_id, client_secret, callback) | |
DropboxAPI.http_request("1/oauth2/token", callback, { | |
method = "POST", | |
headers = { | |
Authorization = "Basic " .. Base64.encode(client_id .. ":" .. client_secret), | |
["Content-Type"] = "application/x-www-form-urlencoded" | |
}, | |
data = argstouri({ | |
code = code, | |
grant_type = "authorization_code" | |
}) | |
}) | |
end | |
function DropboxAPI.account_info(callback) | |
DropboxAPI.http_request("1/account/info", callback) | |
end | |
function DropboxAPI.files.get(root, path, callback, rev) | |
local url = DropboxAPI.locations.api_content .. "1/files/"..root.."/"..path | |
if rev then url = url .. "?rev="..rev end | |
DropboxAPI.http_request(url, callback, nil, true) | |
end | |
function DropboxAPI.files.put() | |
end | |
function DropboxAPI.files.post() | |
end | |
function DropboxAPI.metadata(root, path, callback) | |
DropboxAPI.http_request("1/metadata/"..root.."/"..path, callback) | |
end | |
function DropboxAPI.delta(callback, cursor, locale) | |
DropboxAPI.http_request("1/delta", callback, { | |
method = "POST", | |
headers = { | |
["Content-Type"] = "application/x-www-form-urlencoded" | |
}, | |
data = argstouri({ | |
cursor = cursor, | |
locale = locale | |
}) | |
}) | |
end | |
function DropboxAPI.revisions() | |
end | |
function DropboxAPI.restore() | |
end | |
function DropboxAPI.search() | |
end | |
function DropboxAPI.shares() | |
end | |
function DropboxAPI.media() | |
end | |
function DropboxAPI.copy_ref() | |
end | |
function DropboxAPI.thumbnails() | |
end | |
function DropboxAPI.chunked_upload() | |
end | |
function DropboxAPI.commit_chunked_upload() | |
end | |
function DropboxAPI.fileops.copy() | |
end | |
function DropboxAPI.fileops.create_folder() | |
end | |
function DropboxAPI.fileops.delete() | |
end | |
function DropboxAPI.fileops.move() | |
end | |
-- | |
function DropboxAPI.http_request(url, callback, opts, fullUrl) | |
opts = opts or {} | |
if DropboxAPI.token then | |
if opts.headers then | |
opts.headers.Authorization = "Bearer " .. DropboxAPI.token | |
else | |
opts.headers = {Authorization = "Bearer " .. DropboxAPI.token} | |
end | |
end | |
-- if opts.data then opts.data = json.encode(opts.data) end | |
local _url | |
if (fullUrl) then _url = url else _url = DropboxAPI.locations.api .. url end | |
http.request(_url, function(data, status, headers) | |
for k,v in pairs(DropboxAPI.errors) do | |
if status == tonumber(k) then | |
print(v) -- error ? | |
break | |
end | |
end | |
-- if status == 500 then error("Dropbox: Internal Server Error ...") end | |
if headers["Content-Type"]:find("text/javascript") then | |
data = json.decode(data) | |
end | |
callback(data, status, headers) | |
end, alert, opts) | |
end | |
function argstouri(args) | |
local uri | |
for k,v in pairs(args) do | |
if uri == nil then | |
uri = "" .. k .. "=" .. v | |
else | |
uri = uri .. "&" .. k .. "=" .. v | |
end | |
end | |
return uri | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment