Google Drive api Android download Google doc file - android

I am trying to download files from Google drive. I am using the JAVA API for on android since Google says it provides more control over Google Drive.
So far I can download files, but I cannot download Google doc files.
I assume this is because in order to download a Google doc file it needs to be converted to PDF/Word/HTML before I can download it and the same reason why the size is unknown/0.
So my question is how can convert a Google doc to a word document and download it?

See Google REST API
https://developers.google.com/drive/web/manage-downloads
The Drive API allows you to download files that are stored in Google Drive. Also, you can download Google Documents (Documents, Spreadsheets, Presentations, etc.) and export them to formats that your app can handle. Drive also supports providing users direct access to a file via a link.

I assume you are looking for URL to query for exporting Doc type
url:'https://www.googleapis.com/drive/v3/files/' + fileId + '/export?mimeType=application/vnd.openxmlformats-officedocument.wordprocessingml.document'
method: GET
Content-Type: 'application/json'
Authorization: 'Bearer <Your oauth token>'
Alter mimeType parameter your required value as per allowed mime types here.
Response body contains the exported document.
You can test parameters with following wget command. It will export document in result.doc
wget --header="Authorization: Bearer <your Oauth token>" --header="Content-Type: application/json" --method=GET 'https://www.googleapis.com/drive/v3/files/{FileID}/export?mimeType=application/vnd.openxmlformats-officedocument.wordprocessingml.document' -O result.doc

Mimetypes are used to specify which format you want to download the file in.
So the problem was there was a space in my MimeType... that was giving me the error.
But use the following link for converting to different formats. Scroll to where it says Downloading Google Documents
Link

Related

How to upload files using the Google Drive API?

I'm trying to upload a file using the Google Drive api on Android
https://github.com/googlesamples/google-services/tree/master/android/signin/app/src/main/java/com/google/samples/quickstart/signin
I signed up to SignInActivityWithDrive.java in the link above.
But there is no example of uploading a file, downloading a file
I want to know how to upload and download files
Thank you
You can find basic examples of uploading and downloading in the docs.
Uploading
You can send upload requests in any of the following ways:
Simple upload: uploadType=media. For quick transfer of a small file (5 MB or less). To perform a simple upload, refer to
Performing a Simple Upload.
Multipart upload: uploadType=multipart. For quick transfer of a small file (5 MB or less) and metadata describing the file, all in
a single request. To perform a multipart upload, refer to
Performing a Multipart Upload.
Resumable upload: uploadType=resumable. For more reliable transfer, especially important with large files. Resumable uploads
are a good choice for most applications, since they also work for
small files at the cost of one additional HTTP request per upload.
To perform a resumable upload, refer to Performing a Resumable
Upload.
The following example shows how to upload an image using the client libraries:
File fileMetadata = new File();
fileMetadata.setName("photo.jpg");
java.io.File filePath = new java.io.File("files/photo.jpg");
FileContent mediaContent = new FileContent("image/jpeg", filePath);
File file = driveService.files().create(fileMetadata, mediaContent)
.setFields("id")
.execute();
System.out.println("File ID: " + file.getId());
Downloading
Depending on the type of download you'd like to perform — a file, a
Google Document, or a content link — you'll use one of the following
URLs:
Download a file — files.get with alt=media file resource
Download and export a Google Doc — files.export
Link a user to a file — webContentLink from the file resource
An example of a basic download is:
String fileId = "0BwwA4oUTeiV1UVNwOHItT0xfa2M";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().get(fileId)
.executeMediaAndDownloadTo(outputStream);
It is 2022 now, and how the Google Drive API works might have changed significantly. I needed to upload a number of large files from a remote server where I have terminal access. This is how I got it working for me:
Use the steps detailed in this link to create a Google Services API (on your local computer) and get the API credentials. An extra step was required before step 3, go to the 'OAuth consent screen' tab on the panel to the left and complete necessary steps required. You have to do this only once. For free google accounts, you'll have to select External as the API type (but you can always keep the api in testing mode to not allow others to use it). Also add the gmail address you wish to use as a test user in this panel. Continue the rest of the steps from the aforementioned link.
From Step 1 you should get a client_secret_XXXXX.json file. Copy it to your remote computer working directory using SCP. Rename the file to client_secrets.json.
pip install pydrive
Import and run the following inside the remote working directory.
from pydrive.auth import GoogleAuth
gauth = GoogleAuth()
gauth.CommandLineAuth()
It will provide you a link that you can use to log into your google account from your local computer. You will get a login key that you will have paste into your remote terminal.
Upload a list of filenames
from pydrive.drive import GoogleDrive
drive = GoogleDrive(gauth)
for filename in filename_list:
## Enter folder ID here.
## You can get the folder Id from your drive link e.g.,
## https://drive.google.com/drive/u/2/folders/1pzschX3uMbxU0lB5WZ6IlEEeAUE8MZ-t
gfile = drive.CreateFile({'parents': [{'id': '1pzschX3uMbxU0lB5WZ6IlEEeAUE8MZ-t'}]})
gfile.SetContentFile(filename)
gfile.Upload() # Upload the file.

Access to excel file via microsoft graph on android

I am trying to make an app for my organization which manages its onedrive files via an android app
I would like the manager of the app to maintain the app via a simple excel file which is located on my onedrive to which he will have write access and the rest of the users will have read access only. In other words, i need the app to be able to get the JSON representation of a selected excel file on my onedrive
i downloaded the "active-directory-android-native-v2-master" android sample and run it after obtaining an app ID and it worked correctly while getting the JSON response by executing the next MSGRAPH_URL constant
https://graph.microsoft.com/v1.0/me
Now, i guess that to get my excel JSON, i just need to modify the MSGRAPH_URL to the correct URL but i am getting very strange 404 errors when looking on the debug console (and the app seems to be stuck)
I tried modifying the app permissions via the microsoft app settings screen but with no success.
So the question is,
lets say the file i want to access is the one at the below link. What should be the correct MSGRAPH_URL constant and do i need to modify anything else? Also, is there a site or something that converts such a link to the correct graph.microsoft link?
excel file
According to your descriptions, I assume you want to get the excel file by the Graph API and then modify them.
We can use following steps:
We can modify the permissions at this site. According to this document, we should granted permission to 'Files.Read, Files.ReadWrite, Files.Read.All, Files.ReadWrite.All, Sites.Read.All, Sites.ReadWrite.All'.
Then use this API: 'https://graph.microsoft.com/v1.0/me/drive/root/children' to get the file on your OneDrive.
This API will return all the file on your OneDrive, you can find the excel file in the return. The result will be somthing like this:'
"id": "{The Id for the Excel File }",
"lastModifiedDateTime": "2018-09-03T06:05:59Z",
"name": "Book.xlsx",
"webUrl": "{The WebUrl for the Excel File }",
"cTag": "\"c:{94047B18-87D4-40A6-82C8-B0A0AE2344D5},9\"",
"size": 8252,
Now we can refer to this document to get the worksheet and use the following Docs to modify the Worksheet.

Download Google doc file to device

I am trying to download a doc.google file to my device, however I can't figure the best way for that..
First: I created an Auth key to my Drive following this tutorial
Then: I am trying to follow this tutorial to download the file, but It is not going well
I want to connect to this Auth key through the app and download/Export the file automatically without prompting the user to choose account or anything..
This is the URL for the doc file:
https://docs.google.com/spreadsheets/d/1QJTna4iz-VivwAzwgq7V5QDs2XbgM2lEFPEG7NqCPdo/
Thanks in advance
Try the method used in this SO post.
It was cited that to overcome the google problem when setting the file publicly. As in the post:
If you set the file permissions to be publicly available and
create/generate a direct access link by using something like the
gdocs2direct tool or just crafting the link yourself:
https://docs.google.com/uc?export=download&id=<your file id>
You will get a cookie based verification code and prompt "Google could
not scan this file" prompt, which won't work for things such as
wget or Vagrantfile configs.
The code that it generates is a simple code that appends GET query
variable ...&confirm=### to the string, but it's per user specific,
so it's not like you can copy/paste that query variable for others.
The specified web page hosting method will suffice.

Get file from Google Drive via shared link

I'm making something like online journal app, that will download "journal file" from Google Drive (via shared link) once and will update that file if it changes. Please, can anyone point me to some guides how to do it. I already tried to pin file from drive, but I don't really understand what to do next..
Downloading Files from Google Drive:
To download a Google Drive file via link, try this (from this tutorial):
https://drive.google.com/uc?export=download&id=FILE_ID
Just replace the FILE_ID with the original fileID found in the Drive URL.
Additonal notes:
You can download files using the DRIVE REST API
To download files, you make an authorized HTTP GET request to the file's resource URL and include the query parameter alt=media. For example:
GET https://www.googleapis.com/drive/v3/files/0B9jNhSvVjoIVM3dKcGRKRmVIOVU?alt=media
Authorization: Bearer ya29.AHESVbXTUv5mHMo3RYfmS1YJonjzzdTOFZwvyOAUVhrs
Downloading the file requires the user to have at least read access. Additionally, your app must be authorized with a scope that allows reading of file content. For example, an app using the drive.readonly.metadata scope would not be authorized to download the file contents. Users with edit permission may restrict downloading by read-only users by setting the viewersCanCopyContent field to true.
Updating files in Google Drive
Make an HTTP Request to Google Drive using PATCH. The PATCH method requests that a set of changes described in the request entity be applied to the resource identified by the Request- URI. Things to take note of are:
Parameters
Authorization
Request Body

Downloading Image from google drive api

is there any way to download image from drive pro-grammatically in API guide there is no info to download i do not want to pin image for pin image the drive file chores prompt displays
A quick look on google found me this:
Download image file from Google Drive
Might be worth checking up.
Yes, definitely.
You can use an http GET request:
GET https://www.googleapis.com/drive/v3/files/0B9jNhSvVjoIVM3dKcGRKRmVIOVU?alt=media Authorization: Bearer ya29.AHESVbXTUv5mHMo3RYfmS1YJonjzzdTOFZwvyOAUVhrs
You request the files resource URL. This tool let's you test calling a live file to make sure it works.
https://developers.google.com/drive/v3/reference/files/get#try-it

Categories

Resources