0

Looking for a way to download specific files from a shared Google Drive. Something along the lines of a batch file would be great. Run it and it will download the 5/6 files needed and put them into a network drive location. Using Native windows stuff, no specific files (work related so restrictions on downloadss etc.).

I've tried a few things, but can't seem to get my head around it.

Things I have tried so far:

Tried using PowerShell, in various forms to do this. But have a funny feeling that the corporate proxy is blocking it from working.

Have tried:

$client = new-object System.Net.WebClient 
$client.Credentials = Get-Credential
$client.Proxy.Credentials[System.Net.CredentialCache]::DefaultNetworkCredentials
$client.DownloadFile("https://docs.google.com/a/mydomain.co.uk/spreadsheets/d/1in0m8PhfiYhu4qCWO1dxNc3OS3p8prF7HWRZ-bjnKBI/export?format=xlsx","W:\Corp\Comp Serv\Comp Op\Op Br\Dep Data\Call\Rota\WTR.xlsx")

And:

$source = "https://docs.google.com/a/mydomain.co.uk/spreadsheets/d/1in0m8PhfiYhu4qCWO1dxNc3OS3p8prF7HWRZ-bjnKBI/export?format=xlsx"
$dest = "W:\Corp\Comp Serv\Comp Op\Op Br\Dep Data\Call\Rota\WTR.xlsx"
$WebmyClient = New-Object System.Net.WebClient
$WebProxy = New-Object System.Net.WebProxy("http://myproxy.com:1111",$true)
$Credentials = New-Object Net.NetworkCredential("user,"","domain.local")
$Credentials = $Credentials.GetCredential("http://myproxy.com","1111", "KERBEROS");
$WebProxy.Credentials = $Credentials
$WebClient.Proxy = $WebProxy
$WebClient.DownloadFile($source,$dest)

Tried doing this using bitsadmin, but have had no luck, it returned - 'Unable to add file - 0x80070005'

bitsadmin /transfer myDownloadJob /download /priority normal https://docs.google.com/a/mydomain.co.uk/spreadsheets/d/1in0m8PhfiYhu4qCWO1dxNc3OS3p8prF7HWRZ-bjnKBI/export?format=xlsx W:\Corp\Comp Serv\Comp Op\Op Br\Dep Data\Call\Rota\WTR.xlsx

Tried doing it via batch but no joy. Using this script I found online:

   @echo off
    setlocal

    :: uses bitsadmin utility to download a file
    :: bitsadmin is not available in winXP Home edition
    :: the only way to download a file with 'pure' batch
   :download

    if "%2" equ "" (
      call :help
      exit /b 5
   )

   if "%1" equ "" (
      call :help
      exit /b 6
   )
    set url=%~1
    set file=%~2
    rem ----
    if "%~3" NEQ "" (
        set /A timeout=%~3
    ) else (
        set timeout=5
    )

    bitsadmin /cancel download >nul
    bitsadmin /create /download download >nul 
    call bitsadmin /addfile download "%url%" "%CD%\%file%" >nul
    bitsadmin /resume download >nul 
    bitsadmin /setproxysettings download AUTODETECT >nul

    set /a attempts=0
    :repeat
    set /a attempts +=1
    if "%attempts%" EQU "10" (
        echo TIMED OUT
        endlocal
        exit /b 1
    )
    bitsadmin /info download /verbose | find  "STATE: ERROR"  >nul 2>&1 && endlocal &&  bitsadmin /cancel download && echo SOME KIND OF ERROR && exit /b 2
    bitsadmin /info download /verbose | find  "STATE: SUSPENDED" >nul 2>&1 && endlocal &&  bitsadmin /cancel download &&echo FILE WAS NOT ADDED && exit /b 3
    bitsadmin /info download /verbose | find  "STATE: TRANSIENT_ERROR" >nul 2>&1 && endlocal &&  bitsadmin /cancel download &&echo TRANSIENT ERROR && exit /b 4
    bitsadmin /info download /verbose | find  "STATE: TRANSFERRED" >nul 2>&1 && goto :finishing 

   w32tm /stripchart /computer:localhost /period:1 /dataonly /samples:%timeout%  >nul 2>&1
    goto :repeat
    :finishing 
    bitsadmin /complete download >nul
    echo download finished
    endlocal
   goto :eof

   :help
   echo %~n0 url file [timeout]
   echo.
   echo  https://docs.google.com/a/domainname.co.uk/spreadsheets/d/1in0m8PhfiYhu4qCWO1dxNc3OS3p8prF7HWRF-bjnKBI/export?format=xlsx
   echo  W:\Corp\Comp Serv\Comp Op\Op Br\Dep Data\Call\Rota\WTR.xlsx
   echo  10
   echo.
   goto :eof
Zig Mandel
  • 19,571
  • 5
  • 26
  • 36
SL8t7
  • 617
  • 2
  • 9
  • 27
  • Totally possible. If you were to post what you tried you would receive help. – Zig Mandel Mar 11 '15 at 14:46
  • Was in a hurry last night. I will update the post. – SL8t7 Mar 11 '15 at 15:10
  • Edits have now been made. – SL8t7 Mar 11 '15 at 15:51
  • None of the,ways you tried use a way documented by google. Look at google drive api and or spteadsheets api – Zig Mandel Mar 11 '15 at 15:53
  • The corperate account we have doesn't give me access to the APIs, which is why... – SL8t7 Mar 11 '15 at 16:12
  • Whyxdo you mean by "does not give me access"? If you can use drive, you have access. Or is googleapis.com blocked? Explain in detail. – Zig Mandel Mar 11 '15 at 16:15
  • after you explain this better, I can vote to remove the "too broad" reason for being on-hold, and offer an answer to this without using the api or api urls. – Zig Mandel Mar 11 '15 at 16:27
  • "Developers Console has not been activated for your account. Your account may be suspended or disabled. If you are a Google Apps user, ask your domain administrator to enable Apphosting Admin on your account." - http://i.imgur.com/KjQzckA.jpg – SL8t7 Mar 11 '15 at 16:50
  • is this by policy? its possible to activate it to an org unit only so its absurd that someone in charge of backups does not have this enabled. see http://stackoverflow.com/questions/27114798/how-to-create-a-project-in-google-developers-console – Zig Mandel Mar 11 '15 at 17:07
  • Yes, its policy. :/ I don't have the abillity to do anything more or gain any futher privalages on the account. I'm not strictly in charge of backups, but its a project I am working on. The file needs to be ran from any of the PCs in our office (4) by any of the 22 or so staff members (depending on who is on shift at that time). – SL8t7 Mar 11 '15 at 17:18

1 Answers1

0

Assuming that you cannot enable api access to your account (see https://stackoverflow.com/a/27120094/2213940 where it shows how to enable api access to just an organization within your domain) you could do the following:
Using a browser debugger (I used chrome which opens using F12) go to your drive, and select one of the files you want to download.
In the chrome debugger, view the "Network" tab. clear it to make things easier.
Now download the file. If its a non-google file, just download it from there. You will see a post like "https://drive.google.com/uc?id=XXXXXXXXXX&authuser=0&export=download". You can use curl to do the same post, right-click the line and "copy as cURL" which will copy the cookie and such.
that post gives you a download url.

if its a google file (like a spreadsheet) you need to do something similar but it might only work if you do it by opening the spreadsheet first and using the menu to export as xlsx which will be the best backup format.
those urls will look like "https://docs.google.com/spreadsheets/d/XXXXX/export?format=xlsx&id=XXXX

Note that in both cases, the key is to include the same cookie that your browser is using. Ive used cookies like that (but for gmail not drive) and the cookie so far is lasting months and hasnt expired). Make sure to secure the scripts well as the cookie is for your own drive and can be used maliciously.

Community
  • 1
  • 1
Zig Mandel
  • 19,571
  • 5
  • 26
  • 36
  • Since your comments say a number of people would need access to your script, be aware that the cookie is for YOUR drive, and thus a malicious user could share it or use it to take control of your drive. – Zig Mandel Mar 11 '15 at 17:26
  • Nope, no option to enable API access, so I've gone and got the cURL link for one of the Spreadsheets. The link works fine, and downloads the spreadsheet without problem. Now I need a way to specify the file location (on a network drive), and incorperate it into a batch. I use START firefox.exe **cURL address* and that will prompt and download it fine. Ideally, I'd have no browser open for it and it just download the files quietly in the background. – SL8t7 Mar 11 '15 at 17:58
  • curl can be used from a script, not just from a browser (help regarding curl is not part of this question) – Zig Mandel Mar 11 '15 at 18:00
  • So. I'm to open another question to get the answer to my question? – SL8t7 Mar 11 '15 at 18:53
  • Google it first. Its a basic question explained in many sites. – Zig Mandel Mar 11 '15 at 18:55