Google Drive API create and download PDF - google-drive-api

Is it possible to call the Google Drive API, create a document based on a template with dynamic data sent from an app that is not a Google Apps Scripts app, and download the document as a PDF.

Based on the Google Drive documentation, The Drive API allows you to download files that are stored in Google Drive. Also, you can download exported versions of Google Documents (Documents, Spreadsheets, Presentations, etc.) in formats that your app can handle. Drive also supports providing users direct access to a file via the URL in the webViewLink property.
Here is an example code that demonstrate on how to download a Google Document in PDF format using the client libraries:
String fileId = "1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().export(fileId, "application/pdf")
.executeMediaAndDownloadTo(outputStream);
Also check this page for more information.

The documentation has instructions on doing this via an HTTP request. However, it is silent on doing it via the Drive V3 Python API.
I found the solution in this video Google Drive API: Uploading & Downloading Files, released by the official Google Developers YouTube channel.
This example assumes that you have a file created and that you have the authorization setup.
# setup authorization
DRIVE = discovery.build('drive', 'v3', credentials=creds)
file_id = 'your-file-id'
data = DRIVE.files().export(fileId=file_id, mimeType="application/pdf").execute()
if data:
filename = 'your-file-name.pdf'
with open(filename, 'wb') as pdf_file:
pdf_file.write(data)
The file will be downloaded in the current working directory.

I am wondering, if by using that API you can also define which Workbook you want to retrieve and whether the export should be portrait or landscape.
I found out, that you can set key (string)/value (object) settings to the request, but it actually does not work.
Anybody knows how to fine-tune the export via the API. If I am logged in, I can do it via a simple HTTP Get like that (below), but how to do it with the API?
https://docs.google.com/feeds/download/spreadsheets/Export?key=<spreadheetIDHere>&exportFormat=pdf&gid=<workbookIdHere>
best,
fri

Related

Google Drive API no longer working to list files in folder

I have some Perl code that retrieves a list of files from a specified folder on my Google drive using the following GET URL:
https://www.googleapis.com/drive/v3/files?q='[FOLDER_ID]'+in+parents&fields=files(name,webViewLink,thumbnailLink,videoMediaMetadata/durationMillis)&key=[GOOGLE_DRIVE_API_KEY]
I have not changed anything recently, but today I noticed that the using this URL returns an empty files array. The response code is "200" and the content body of the response is:
{
"files": []
}
I have validated that the folder ID is still correct according to Google Drive.
I have validated that my Google Drive API key is still enabled and correct.
I have validated that the files do exist in that folder on the Google Drive.
Has something changed on the Google API side that would cause this API request to stop returning the list of files in the specified folder?
Thanks!
Finally figured this out: I needed to pass the
X-Goog-Drive-Resource-Keys
HTTP header with the request, and set its value to:
[FOLDER_ID]/[FOLDER_RESOURCE_KEY]
Apparently, as part of Google's Drive API security update this month, links that are shared for "everyone with the link" now require this HTTP header and the resource ID/key to access those shared files.

When/why can file.downloadUrl be empty for a PDF file selected from Google Drive?

Using js file picker to select PDF files from Google Drive.
I'm encountering an instance when the downloadUrl is undefined.
I don't understand what can cause this.
I know downloadUrl can be empty for native Google formats, but these are PDF files.
I'd like to know what scenarios (except native Google formats) can cause an empty downloadUrl.
Thanks
You should use the Drive API to fetch the picked files' metadata in order to retrieve the downloadUrl. https://developers.google.com/drive/v2/reference/files/get
Native Google formats can be downloaded (converted and downloaded) via their exportLinks: https://developers.google.com/drive/v2/reference/files#exportLinks
The response I was receiving was 403.
And the scenario was user accessing "work" Drive (Google Drive for your domain)
Turns out Google Drive for your domain Administrators can restrict the permission to install third party apps.
https://support.google.com/a/answer/6105699?hl=en
User will still be able to allow access for the app and select files. Its just going to fail every time with a 403. Very confusing UX.

Oauth2 Google Drive offline access not working for non-google app files?

Having a very frustrating issue with Google Drive API.
This is a Google App Engine Java environment.
I need to download a file based on an offline permission - the file is shared ("Can Edit") with the user who gives the offline permission. I am requesting full https://www.googleapis.com/auth/drive scope. I store off the refresh token this generates.
The app then uses this refresh token to get an access token, which it uses to try and download a file. This works file if the file is a Google Apps file (a Google document, spreadsheet etc.) but gives a 401 if the file is an uploaded file with content (e.g. a PDF file, Word file etc.). I can confirm this at a simple level by appending the share urls with ?access_token=xxxx - this works for the Google Apps file but not for an uploaded normal file, which 401's on the webcontentlink url. Note that the https://www.googleapis.com/drive/v2/files/ endpoint responds correctly with the metadata for the uploaded file using the access token that subsequently fails on the download call.
The full html response from either a direct url call (with access_token=) or a servlet call is
<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
Is there a known issue with offline Google auth on non native files in Drive?
Based on the descriptions of file attributes, you should probably be using the downloadUrl attribute rather than webContentLink.
downloadUrl - Short lived download URL for the file. This is only populated for files with content stored in Drive.
webContentLink - A link for downloading the content of the file in a browser using cookie based authentication. In cases where the content is shared publicly, the content can be downloaded without any credentials.

google drive preview generation while uploading document

Can a google drive application generate a preview for a file (which is not handled by google drive by default) when a file of that type is uploaded by the user, rather than creating it using that application?(Assuming the user has authorized that application)
Unfortunately, no, sorry.
The closest we have is being able to generate a third party thumbnail for a file. Your app can decide how the file will appear in the Google Drive thumbnail view by uploading the necessary image(s).
The video here explains it: http://www.youtube.com/watch?v=jG5-9zlaPL8

Copy a file from Google Drive to my own server

If you remember, I'm trying to integrate Google Drive within our website, which is built on Elgg. Elgg already has its native file management system.
What we would like to do is to copy a file from Drive to our server, you know, kind of : Send to My Files. The problem is that I don't see any URL in the file metadata indicating where the file is physically stored.
I can see the copy function in Google Drive SDK but I don't think it allows to copy the file on our own server. Unless I've read it wrong.
Can you help me?
Thanks you.
If you are trying to save a file that has content stored in drive (e.g an image, pdf, etc.), the file's metadata should contain a downloadUrl that can be used to retrieve the file's content through an authorized GET request.
For Google Documents (Google Docs, Google Spreadsheets, etc.), the data is stored in a private format that cannot be understand by third party applications. In this case, your app will have to use one of the exposed exportLinks to export the document into a format understood by your application.