Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download public file from Google Drive - Golang

I have a zip file stored on Google Drive (it is shared publicly). I want to know how to download it in Golang. This current code just creates a blank file named "file.zip":

package main

import (
    "fmt"
    "io"
    "net/http"
    "os"
)

func main() {
    url := "https://docs.google.com/uc?export=download&id=0B2Q7X-dUtUBebElySVh1ZS1iaTQ"
    fileName := "file.zip"
    fmt.Println("Downloading file...")

    output, err := os.Create(fileName)
    defer output.Close()

    response, err := http.Get(url)
    if err != nil {
        fmt.Println("Error while downloading", url, "-", eerrror)
        return
    }
    defer response.Body.Close()

    n, err := io.Copy(output, response.Body)

    fmt.Println(n, "bytes downloaded")
}
like image 572
DanielTA Avatar asked Aug 11 '13 22:08

DanielTA


People also ask

How do I download a file from Google Drive programmatically?

To download a file stored on Google Drive, use the files. get method with the ID of the file to download and the alt=media URL parameter. The alt=media URL parameter tells the server that a download of content is being requested.

Can I download files from Google Drive?

Download files from Google Drive with a computer, Android, or iOS device. Important: If you try to download a suspicious file, you may get a warning message. Use caution if you download the file.

How do I download Google Drive credentials JSON?

Go to Google API Console. Go to the Credentials page. Click the Download JSON button to download the client secret JSON file and securely store it in a local folder. This JSON file can then be used by Google Drive components and metadata wizard to access Google Drive via the OAuth method Installed Application (JSON) .


2 Answers

This appears to be a bug, either with Google drive or with golang, I'm not sure which!

The problem is that the first URL you gave redirects to a second URL which looks something like this

https://doc-00-c8-docs.googleusercontent.com/docs/securesc/ha0ro937gcuc7l7deffksulhg5h7mbp1/8i67l6m6cdojptjuh883mu0qqmtptds1/1376330400000/06448503420061938118/*/0B2Q7X-dUtUBebElySVh1ZS1iaTQ?h=16653014193614665626&e=download

Note the * in the URL which is legal according to this stack overflow question. However it does have a special meaning as a delimeter.

Go fetches the URL with the * encoded as %2A like this

https://doc-00-c8-docs.googleusercontent.com/docs/securesc/ha0ro937gcuc7l7deffksulhg5h7mbp1/8i67l6m6cdojptjuh883mu0qqmtptds1/1376330400000/06448503420061938118/%2A/0B2Q7X-dUtUBebElySVh1ZS1iaTQ?h=16653014193614665626&e=download

Which Google replies "403 Forbidden" to.

Google doesn't seem to be resolving the %2A into a *.

According to this article on wikipedia reserved characters (of which * is one) used in a URI scheme: if it is necessary to use that character for some other purpose, then the character must be percent-encoded.

I'm not enough of an expert on this to say who is right, but since Google wrote both parts of the problem it is definitely their fault somewhere!

Here is the program I was using for testing

like image 102
Nick Craig-Wood Avatar answered Sep 28 '22 00:09

Nick Craig-Wood


I found the solution. Use: https://googledrive.com/host/ID

Instead of: https://docs.google.com/uc?export=download&id=ID

like image 41
DanielTA Avatar answered Sep 28 '22 00:09

DanielTA