Skip to content

Instantly share code, notes, and snippets.

@rinx
Created February 6, 2016 13:46
Show Gist options
  • Save rinx/781dc459ff2778ed6b86 to your computer and use it in GitHub Desktop.
Save rinx/781dc459ff2778ed6b86 to your computer and use it in GitHub Desktop.
flashair to google drive with apiv3
local cjson = require "cjson"
-- Basic account info
local client_id = ""
local client_secret = ""
local refresh_token = ""
local boundary = "--61141483716826"
local function getAuth()
local mes="client_id="..client_id
mes=mes.."&client_secret="..client_secret
mes=mes.."&refresh_token="..refresh_token
mes=mes.."&grant_type=refresh_token"
local length = string.len(mes)
print("Sending: ["..mes.."]\n")
b, c, h = fa.request{
url = "https://accounts.google.com/o/oauth2/token",
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
["Content-Length"] = length,
},
method = "POST",
body=mes,
}
print("sended, header:"..h.."\n")
local tempTable = {}
tempTable = cjson.decode(b)
local access_token = tempTable["access_token"]
print("access_token:"..access_token.."\n")
return (access_token)
end
local function uploadTest(token, filePath)
local mes = "--"..boundary.."\r\n"
mes=mes.."Content-Type: application/json; charset=UTF-8\r\n"
mes=mes.."\r\n"
mes=mes.."{\r\n"
mes=mes.." \"name\": \"".."testfile_uploaded.jpg".."\"\r\n"
mes=mes.."}\r\n"
mes=mes.."\r\n"
mes=mes.."--"..boundary.."\r\n"
mes=mes.."Content-Type: image/jpeg\r\n"
mes=mes.."\r\n"
mes=mes.."<!--WLANSDFILE-->\r\n"
mes=mes.."--"..boundary.."--\r\n"
print("sending:"..filePath.."\n")
b, c, h = fa.request{
url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
headers = {
["Content-Type"] = "multipart/related; boundary="..boundary,
["Content-Length"] = tostring(lfs.attributes(filePath,"size") + string.len(mes) - 17),
["Authorization"] = "Bearer "..token,
},
method = "POST",
file = filePath,
body = mes
}
print("sended, header:"..h.."\n")
print("sended, body:"..b.."\n")
print("sended:"..filePath.."\n")
end
access_token = getAuth()
collectgarbage()
uploadTest(access_token, "/lua/testfile.jpg")
collectgarbage()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment