Download public file from Google Drive - Golang

I have a zip file stored in Google Drive (it is publicly available). I want to know how to download it in the Golang. This current code simply creates an empty file called "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") } 
+6
source share
3 answers
+5
source

This seems to be a mistake, either with Google drive or with golang, I'm not sure which one!

The problem is that in the first URL, you redirected the 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

Please note the * in the URL which is legal in accordance with this question . However, it is of particular importance as a meter.

Go selects a URL with * 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 does not seem to allow %2A in * .

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

I do not have enough expert on this subject 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 used for testing

+7
source

I am still studying why this is happening, in the meantime you can use this workaround:

http://play.golang.org/p/SzGBAiZdGJ

CheckRedirect is called when a redirect occurs, and you can add an opaque path to avoid the url url.

Francesc

+3
source

Source: https://habr.com/ru/post/951517/


All Articles