ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Lua实现Dropbox上传_艾孜尔江撰

2021-06-22 09:03:24  阅读:224  来源: 互联网

标签:.. -- 艾孜尔江 access Lua token file Dropbox local


--[[
DropboxUpload.lua

]]

--[[
FlashAir Lua Dropbox example.

This script uploads and files in a directory to dropbox, using oauth2.
It will store the access token in dropbox_token.txt
--]]
local tokenfile		= "/dropbox_token.txt" 	-- Where to log output on the FA
local folder 		= "/Upload" 		-- What folder to upload files from
local app_key		= "btbjfXxXxXxXxXx"		-- Your Dropbox app's key
local app_secret	= "4k79gXxXxXxXxXx"		-- Your Dropbox app's secret

--NOTE: Remember to authorize your app!
local auth_code 	= "XrfRXkfTNcXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxX" -- authorization code!

--[[
requestToken(app key, app secret, authorization code)
requests the oath2 token, using fa.request().
returns the token, or nil on a failure.
--]]
local function requestToken(key, secret, auth_code)
	--Request a token
	message = "grant_type=authorization_code&code="..auth_code.."&client_id="..key.."&client_secret="..secret
	b, c, h = fa.request{
		url = "https://api.dropboxapi.com/oauth2/token",
		method = "POST",
		headers = {["Content-Length"] = string.len(message),
		["Content-Type"] = "application/x-www-form-urlencoded"},
		body = message
	}

	--Decode the response body (json format)
	response = cjson.decode(b)
	access_token = response["access_token"]
	if access_token ~= nil then
		return access_token
	else
		error = response["error_description"]
		print("Failed to get access token. Error: "..c..": "..error)
		return nil
	end
end

--[[
These functions simply load or save the access token to/from a file (tokenfile).
--]]
local function saveToken(access_token)
	local file = io.open(tokenfile, "w" )
	file:write(access_token)
	io.close(file)
end
local function loadToken()
	local file = io.open(tokenfile, "r" )
	access_token = nil
	if file then
		access_token = file:read( "*a" )
	end
	return access_token
end

--[[
	uploadFile(folder, file name, access token)
	Attempts to upload a file to dropbox!
--]]
local function uploadFile(folder, file, access_token)
	file_path=folder .. "/" .. file
	--get the size of the file
	local filesize = lfs.attributes(file_path,"size")
	if filesize ~= nil then
		print("Uploading "..file_path.." size: "..filesize)
	else
		print("Failed to find "..file_path.."... something wen't wrong!")
		return
	end

	--Upload!
	b, c, h = fa.request{
		url = "https://content.dropboxapi.com/2/files/upload",
		method = "POST",
		headers = {["Authorization"] = "Bearer "..access_token,
		["Content-Length"] = filesize,
		["Content-Type"] = "application/octet-stream",
		["Dropbox-API-Arg"] = '{"path":"'..file_path..'","mode":{".tag":"overwrite"}}'},
		body = "<!--WLANSDFILE-->",
		bufsize = 1460*10,
		file=file_path
	}

	print(c)
	print(b)

end

--Script starts

--Attempt to load a token from the file
token = loadToken()

--See if we loaded one, if not request one
if token == nil then
	--Request an access token
	print("No token found, attempting to request one...")
	token = requestToken(app_key, app_secret, auth_code)

	--Was it successful?
	if token == nil then
		print("Failed to request token, do you need to authorize?")
		print("Auth url: https://www.dropbox.com/oauth2/authorize?client_id="..app_key.."&response_type=code")
	else
		print("New token: "..token)
		saveToken(token)
	end

end

--Before continuing, make sure we have an access token
if token ~= nil then
	print("INIT, with token: "..token)
	-- For each file in folder...
	for file in lfs.dir(folder) do
		-- Get that file's attributes
		attr = lfs.attributes(folder .. "/" .. file)

		-- Don't worry about directories (yet)
		if attr.mode == "file" then
			--Attempt to upload the file!
			uploadFile(folder, file, token)
		end
	end
end

标签:..,--,艾孜尔江,access,Lua,token,file,Dropbox,local
来源: https://blog.csdn.net/weixin_43867242/article/details/118099183

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有