How to download shared file from Google Drive using a link? - google-drive-api

For example we have a link to Google Drive: https://drive.google.com/file/d/1o4bqvKsGjf_yukzt-zQPtZWtqv21gpmv/view?usp=sharing
This file on someone's drive, not on my own. And how to download this file using API, if I know only a link?

The share with me link that you can get via the google drive web application. is not a true form of sharing.
In order to download a file using the api you must first have permission to access that file. That being said you must have a record on the file with your username (email address) returned by a premissions.list call on the file.
Without it when you do a file.list the file will not show up as you do not have permissions on the file.
The issue with the share with me link is that the Id is not the id of the file. So even if the file was public you would not be able to find it.
To be clear a share with me link to a file on google drive does
not contain the true file id needed for the api
grant any true permissions on the file. meaning that you cant access it via the api.

Related

Google Drive File Name Automatically Replaced when User download the file

Google Drive Screenshot
I have one Video file in my Google Drive with the name Perfect.mp4 and I have created a direct download link of this file with Google Drive API. So User can Download this File Without this Warning ( Google Drive can't scan this file for viruses ) but the problem is
when any user downloads this my video file: Perfect.mp4.
Then The name of my video is automatically renamed with the FIELD ID and the file extension is also gone.
This is My Link: https://www.googleapis.com/drive/v3/files/1nRLV1rEcQddkWJ6nGCCdsCvemSp_mqza?alt=media&key=AIzaSyCj8FQUm-Su55PRIQbTZ2BHc9gY8BDr4vE
The filename of my video is correct in my Google Drive account but When Any User Downloads my video file with this link then only The name of my video is automatically renamed with the FIELD ID and the user sees any random file id name instead of my original file name.
Please help me to solve this problem
You should use the webContentLink:
https://drive.google.com/uc?id=1nRLV1rEcQddkWJ6nGCCdsCvemSp_mqza&export=download.
It is not a good idea to share your API key.

How to setOwner() of file after uploading it to a folder in Drive?

Desired Behaviour
This is my first attempt at a google apps script and I want a user to be able to upload a document to a shared folder (with edit access), but then change the permissions of the file so that the user cannot delete the file and can only view it.
(If this is not specifically possible, I am interested in any method that achieves the same result - ie an admin has edit access to a folder of documents uploaded by users with view only access).
What I've Tried
This answer:
https://stackoverflow.com/a/10821130
Points to a method called setOwner(emailAddress)
So I tried:
// define the target folder to create the file in
var driveFolder = DriveApp.getFoldersByName("My Folder").next();
// create a file in the target folder and set a different owner
var driveFile = driveFolder.createFile(formBlob).setOwner("test#gmail.com");
Current Behaviour
The file ownership is not changing.
Developer Tools > Console error is:
Uncaught ScriptError: Invalid argument: sharing.user
With questions about that particular error here:
Trying to change owner on files in Google Drive
Other Settings
Publish > Deploy as web app > Execute the app as > "User accessing the web app"
Publish > Deploy as web app > Who has access to the app > "Anyone"
According to
Change your sharing settings - Google Drive Help the ownership of uploaded files can't be transferred. For an alternative approach see Google Forms file upload complete example
You have to create a google site and use the upload feature then you can change settings.
Since the file one person directly in google is "not yours" you cannot change it. but if you receive it through a site page you will be the owner.

Move file from appDataFolder to user's root folder with Google drive API

Is it possible to move file from appDataFolder to user's root folder on Google Drive using drive API v2 or v3? I can't find any example how to do that. I just try to use gapi.client.drive.files.update from javascript drive v3 API and addParents parameter to change folder, it works fine with files in user's root folder, but doesn't work with files in appDataFolder.
I know that it is possible to copy file from appDataFolder to user's drive root, but I need to keep fileId, and copying generates new fileId for copied file.
I found there is a file property called "spaces" and files from appDataFolder a in spaces=appDataFolder whereas files from user's root folder are in spaces=drive. Is it possible to move file between these spaces keeping same fileId?
I found some similar posts:
Copy an exising Drive file into the appdata folder
Is it possible to share the application data on google drive
and it looks like it is not possible to do it this way. When I check my console I also get "Method not supported for appdata contents" or "Method not supported for files within the Application Data folder." message.
So is there any method to move file from appDataFolder?
Thank's for help.
No, it seems to be impossible. The current document doesn't mention about that, but the error message clearly says so. Thumb down for drive api.

Google Drive API - share(copy) my file automatically to user drive account

the scenario is like this. I have a file in Google Drive, then share it to public, but not the direct link to my file. see sample here https://ganool.ag/steve-jobs-2015-bluray-720p-ganool-ag/ | scroll down and find link https://file.rocks/file/jBH0tM97Ne (ignore the pirate movie in the link, it's just sample). When you click the link, the file will be copied to your drive account, save in a folder called File.rocks, than a subfolder with random name.
my question is, what API command used in that script? just the concept, not the complete script.
sorry for my english.
The behavior shown in that website is similar to the Save to Drive button which allows the user to save files to their Drive account from an arbitrary URL via their browser.
The technology used to upload files is similar to that used by the Google Drive web user interface. The file is downloaded to the user's browser in parts and uploaded to Google Drive as data is received.
Authorize your app to get code
exchange code for token
create client-folder-id if not exist
then use copy
for example:
POST https://www.googleapis.com/drive/v3/files/<your-file-id>/copy
Authorization: Bearer token...xxxxxx
{
"parents": [
"<client-folder-id>"
]
}

Add a shared folder to google drive using a share link

Newest Edit:
So here is what i want to accomplish:
1) User installs my application on his computer.
2) User grants my app access to his drive.
3) My app shares a folder from my drive with the user.
The problem in accomplishing part 3) is that my app actually needs write access to my drive, so it can share my folder with the current user. But for security reasons there is no way i can grant my app write acces to my drive.
Now im asking for a possibility to perform part 3) without granting my app write access for my drive.
I would like my installed application to add a shared folder to a users google drive account by only using a share link i generated manually for a folder i've created in my drive. Is it possible to accomplish this using google drive API?
Edit: What i actually want to do is to create a folder in the users drive and link this folder to a folder in my drive, so the user can access the files stored in my drive.
A shortcut to another's shared directory? Google Drive doesn't support it but, you can create an application to handle it.
Create a shortcut to your application.
Add the the link you want your user to be redirected as a custom property to the shortcut file.
When user opens the file from Drive and being redirected to your app, read the custom property you stored.
Redirect user to the the location on the retrieved custom property.