Search the Community
Showing results for tags 'uploader'.
-
I'd like to create a dropbox uploader (and downloader) to provide a simple file sync without using the dropbox app. The following is my current code (cadged and modified from other posts and websites), but I can't even figure out how to get initial authorization. Can anyone help? NB I would be quite happy to use the oauth2 Generated access token if it bypassed a lot of this, but I'm not sure how to do this. (If you can't guess, I know virtually nothing about http calls) Alternatively if there is a working autoit dropbox up/downloader, I'm more than happy not to write my own (but couldn't find one) Thanks #include "WinHttp.au3" ; set the dropbox tokens ;to get the dropbox app_key and app_secret tokens, browse to https://www.dropbox.com/developers/apply?cont=/developers/apps and create a new app If $App_Key = "" Then $File = "" $App_Key = "" $App_secret = "" $Oauth_access_token = "" $Oauth_access_token_secret = "" ; this could possibly the personal Oauth2 Generated access token $App_folder = "" ; this is the name of your app - it's not currently needed in this code ; the folder where the uploaded file will be stored is: Dropbox/Apps/$App_folder EndIf Local $sURL = "https://api.dropbox.com/1/oauth/request_token " & _ "Authorization: OAuth oauth_version=""1.0"", oauth_signature_method=""PLAINTEXT"", " & _ "oauth_consumer_key=""" & $App_Key & """, " & _ "oauth_signature=""" & $App_secret & "&""" $hOpen = _WinHttpOpen() $FileHandle = FileOpen($File) $hConnect = _WinHttpConnect($hOpen, $sURL) $hRequest = _WinHttpOpenRequest($hConnect, "POST") _WinHttpReceiveResponse($hRequest) $sResult = "" Do $sResult &= _WinHttpReadData($hRequest) Until @error ;MsgBox(0, "", $sResult) ConsoleWrite($sResult & @CRLF) _WinHttpCloseHandle($hRequest) _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hOpen) ; response should be: ;oauth_token=<request-token>&oauth_token_secret=<request-token-secret> ;user will have to browse to the following and authorise the app ;https://www.dropbox.com/1/oauth/authorize?oauth_token=<request-token> ; you now need to generate an access token ; POST https://api.dropbox.com/1/oauth/access_token ; Header will be: ;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>" ;reponse will be: ;oauth_token=<access-token>&oauth_token_secret=<access-token-secret>&uid=<user-id> ;now you can access the account and undertake actions ;eg get account info: ;GET https://api.dropbox.com/1/account/info ;Header: ;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>" ; this function doesn't work, it will need to be modified to allow upload func upload($App_Key, $App_secret, $Oauth_access_token, $Oauth_access_token_secret, $File="") trayseticon("C:\Program Files\AutoIt3\Icons\cute panda.ico", -1) ; Get the file to upload Local $sDrive = "", $sDir = "", $sFilename = "", $sExtension = "" local $time=@HOUR & ":" & @MIN & ":" & @SEC local $Random = Random(0,32767,1) ; if a filename isn't passed to the function, then ask the user for one If $File = "" Then $File = fileopendialog("Pick something to upload", @desktopdir, "All (*.*)|Text files (*.txt)" ) EndIf ;message($File) ; Now open the file and load the data into memory local $array = _PathSplit($File,$sDrive, $sDir, $sFilename, $sExtension) ; get the file name and extension Local $i = UBound($array) - 1 local $sFile = fileopen($File,16) Local $data = fileread($sFile) local $size = @extended FileClose($File) ;set up the dropbox upload URL ;to get the dropbox tokens, browse to https://www.dropbox.com/developers/apply?cont=/developers/apps and create a new app ;$array[$i-1] is the file name of the file the user wants to upload and $array[$i] is the extension Local $sUrl = ("https://api-content.dropbox.com/1/files_put/dropbox"&"/"& $array[$i-1] & $array[$i] & _ '?oauth_consumer_key=' & $App_Key & _ " & oauth_token=" & $Oauth_access_token & _ " & oauth_signature_method=PLAINTEXT & oauth_signature=" & $App_secret & "%26" & $Oauth_access_token_secret & _ " & oauth_timestamp=" & $time & _ " & oauth_nonce=" & $RANDOM) ;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>" msgbox(1,"URL",$sUrl) ; now attempt to upload the file with the generated dropbox URL Local $obj = ObjCreate("WinHttp.WinHttpRequest.5.1") ;message("here") $obj.Open("PUT", $sURL, False) If @error Then MsgBox(1,"ERROR","@1unable to upload" & " " &$array[$i-1] & $array[$i]) else ; message("request opened") $obj.SetRequestHeader("User-Agent", "User-Agent=Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0") ;$obj.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded") $obj.SetRequestHeader("Content-Length", $size) ; if _FindNetwork() = "xavier" Then ; $obj.SetProxy(2, "number") ; endif ; message($sFile) $obj.Send($data) ; send the file to uploaded $oReceived = $obj.ResponseText $oStatusCode = $obj.Status If $obj.Status <> $HTTP_STATUS_OK Then ; local $FindError = FindError($oStatusCode) ; message($oReceived) msgbox(0 , "Error", "@2" & $oStatusCode & " :unable to upload " & $array[$i-1] & $array[$i]) else msgbox(0 , "success", "uploaded " & $array[$i-1] & $array[$i]) endif trayseticon("C:\Program Files\AutoIt3\Icons\panda.ico", -1) endif endfunc