In Google V3 API for the Go language, how do I pass alt=media to a Files.Export() call? The V3 website does not state how it works for golang. I am simply trying to convert a Google doc to a text document and store it within the drive. Calling Files.Export().Do() results in an error asking for alt=media.
Asked
Active
Viewed 934 times
1
-
Unfortunately, `alt=media` cannot be used for [the method of "Files: export"](https://developers.google.com/drive/api/v3/reference/files/export). `alt=media` is used for the method of "Files: get". [Ref](https://developers.google.com/drive/api/v3/manage-downloads) So I cannot understand about what you want to do. Can I ask you about the detail of your goal? – Tanaike Oct 31 '20 at 23:42
-
I am trying to convert a Google doc file into a text file, and download it. – Devon Nov 01 '20 at 05:18
-
Thank you for replying. From your replying, I proposed a modified script as an answer. Could you please confirm it? – Tanaike Nov 01 '20 at 05:40
1 Answers
4
I believe your goal and situation as follows.
- You want to download Google Document on Google Drive as a text data using googleapis for golang.
- You have already been able to download the file using Drive API.
Modification points:
- Unfortunately,
alt=media
cannot be used for the method of "Files: export".alt=media
is used for the method of "Files: get". Ref - In order to download Google Document on Google Drive as a text data, it is required to use the method of "Files: export". By the way, when you want to download the files except for Google Docs files (Document, Spreadsheet, Slides and so on), please use the method of "Files: get".
When above points are reflected to your script, it becomes as follows.
Modified script:
documentID := "###" // Please set the Document ID.
mimeType := "text/plain"
filename := "sample.txt"
srv, err := drive.New(client)
if err != nil {
log.Fatalf("Error: %v", err)
}
res, err := srv.Files.Export(documentID, mimeType).Download()
if err != nil {
log.Fatalf("Error: %v", err)
}
file, err := os.Create(filename)
if err != nil {
log.Fatalf("Error: %v", err)
}
defer file.Close()
_, err = io.Copy(file, res.Body)
- In this sample script, it supposes that
client
of your script can be used for downloading the file from Google Drive. Please be careful this. - When this script is run, the Google Document of
documentID
is exported as a text file offilename
.
References:
- Download files
- Files: export
- google-api-go-client
- Go Quickstart
- If you want to see the script for authorizing, you can see the Quickstart for Go.

Tanaike
- 181,128
- 11
- 97
- 165
-
It works! Thank you so much! I had tried printing out the response body before but never copying it into a new file. Printing out the http response body yields a different output than what is displayed in the file after copying the body. Why is this? – Devon Nov 01 '20 at 06:00
-
@Devon Thank you for replying. From your replying, I could understand that your question was resolved. I'm glad for it. About your new question of `I had tried printing out the response body before but never copying it into a new file. Printing out the http response body yields a different output than what is displayed in the file after copying the body. Why is this?`, unfortunately, I cannot understand it. This is due to my poor English skill. I deeply apologize for this. Can I ask you about the detail of your new question? – Tanaike Nov 01 '20 at 08:15
-
@Tanaike I believe he means that he prints in the console the response of the request. What are the main differences you encounter when you get your body content printed in the console vs when you get it downloaded as a text file? Is the text file your desired output or is it the response variable before downloading? Thanks ! – Mateo Randwolf Nov 02 '20 at 09:05
-
@Mateo Randwolf Thank you for your support. By your comment, I could understand about it and can reply it. But, if I misunderstood your comment, I apologize. – Tanaike Nov 02 '20 at 12:31
-
@Devon When you want to show the downloaded data to the console, using `res` from `srv.Files.Export`, please use the following script. `result, _ := ioutil.ReadAll(res.Body)` and `fmt.Println(string(result))`. By this, you can directly see the downloaded data at the console. In this case, the output mimeType is `text/plain`. If that is the PDF, the binary data is shown. So please be careful this. – Tanaike Nov 02 '20 at 12:31
-
1@Tanaike you saved me again today with this answer! You answered another question I posted a few hours ago. – Roger Creasy Mar 09 '22 at 23:09
-
@Roger Creasy Thank you for your comment. I'm glad your issue was resolved. Thank you, too. – Tanaike Mar 10 '22 at 00:16
-
@Tanaike do you know how to download particular sheet of spreadsheet using Files.Export? – Vishal Patel Aug 09 '22 at 09:05