Get file from Google Drive via shared link - android

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

Related

Statement List Generator and Tester rejects its own generated content

So, basically I'm at step 1 of implementing App Links, I stumbled upon this tool:
https://developers.google.com/digital-asset-links/tools/generator
I give it my domain name, my package name, and I copy paste my App package fingerprint from the play store console. it generate an assetlinks.json file for me, I put it as required at https://my-domain/.well-known/assetlinks.json I test downloads, it works, then I ask this tool to test it, it says
No app deep linking permission found for package_name at my_domain.
my logs indicate that GoogleAssociationService came and took the file (200 status response and the correct number of bytes).
So basically I put the good values, it generates it, and then fetch it and tells me it's wrong, what am I missing ? how can he not be happy with what he generated itself ?
When we came across this issue we have also investigated logs of our proxy and have seen that the "assetlinks.json" file had been successfully downloaded.
In our case, the problem was with Content-Type. In our case, it was the "application/octet-stream" type. According to the documentation, Content-Type should have the type "application/json". We have changed the content type and everything started working as expected.
Check the requirements using the link above and verify that all of them are met by crossing off all potential reasons one by one.
Update 2022-10-30: The provided link is not available anymore. I have used a cached version to fetch the information below:
You must publish your JSON verification file at the following
location:
https://domain.name/.well-known/assetlinks.json
Be sure of the following:
The assetlinks.json file is served with content-type application/json.
The assetlinks.json file must be accessible over an HTTPS connection,
regardless of whether your app's intent filters declare HTTPS as the
data scheme.
The assetlinks.json file must be accessible without any
redirects (no 301 or 302 redirects).
If your app links support
multiple host domains, then you must publish the assetlinks.json file
on each domain. See Supporting app linking for multiple hosts.
Do not
publish your app with dev/test URLs in the manifest file that may not
be accessible to the public (such as any that are accessible only with
a VPN). A work-around in such cases is to configure build variants to
generate a different manifest file for dev builds.

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.

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.

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

Google Drive api Android download Google doc file

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

Categories

Resources