Remove a file that was previously added from Shared With Me to My Drive - google-drive-api

Google Drive has a cool feature: one can add a file or folder from Shared With Me to My Drive.
Then I can delete this file from My Drive in the same manner (note that if I delete shared file in web version it won't go to Trash)
However, I am getting 403 Forbidden error while trying to delete this file using Google Drive API (because of insufficient permissions due to that I am not owner of this file). So, as one can see, this is not a simple Delete request. How can I implement this functionality?

Add to My Drive changes the parents collection for the item in question. To change this, you will want to unparent the item rather than delete.
Use the about.get call to retrieve the My Drive ID, then remove that ID from the list of parents in the file resource for the file/folder. Update the file with the new list of parents.

A late answer, but I ran into this issue too. This happens because the file isn't actually copied, it's the same instance. If you remove it from My Drive, it will be removed from the Shared Drive too. And you may lack permission to remove it from the Shared Drive.
If you want to just remove it from My Drive: click on the file. In the right panel, under Details, there is a list of locations. At least the Shared Drive and My Drive is listed there. Click the X to remove it from My Drive, and it will disappear there while preserved on the Shared Drive.

This is what I found. After removing the parent of an item shared with me, do a sharedWithMe search still return the item, even though the item's parent list is empty.
Here is the code:
string sItemId = (this is the Id of the item shared with me);
string sParentId = driveService.About.Get().Execute().RootFolderId;
driveService.Parents.Delete(sItemId, sParentId).Execute();
var parentList = m_driveService.Parents.List(sItemId).Execute();
At this point parentList.Items.Count is 0.
Now when I search sharedWithMe the item is in the return list.

Related

Requesting to get shared drive List id and drive name in google apps script

enter image description hereDrive.Drives.List this method only retrived 10 shared Drive List and not able to retrieve all the Drive List in google apps script.
Please find my below code for your reference.
I need list of drive id and list of drive name but I am getting only 10 drive id and name not all the list.
function driveList()
{
const sharedDrive = Drive.Drives.list().items
.map(drive => ({id:drive.id,name:drive.name}));
drivedata=JSON.stringify(sharedDrive);
driveList=JSON.parse(drivedata);
for(var i=0;i<driveList.length;i++)
{
Logger.log(driveList[i].id);
Logger.log(driveList[i].name);
}
}
Update with a third possible reason
It's probably one of the three reasons:
You need to paginate; if your result has a nextPageToken property, you need to go to the next page and get more results, and continue doing so while you still get a nextPageToken. You likely want to use the do while loop here to make sure your request executes at least once.
You simply don't have access to those other shared folders and might want to be check if don't have a multi-login issue.
Drives.list retrieves shared Drives and not shared folders. Given that you are using both terms interchangeably, the third possibility is that you are expecting folders but getting drives instead

Tracking changes in "shared drive"

I've been using a Google service account to track changes in a folder located in My Drive.
This folder is shared read-only with the service account.
Normal operation
First time I'm running these steps
Get a startPageToken
List all files and save a local copy
Next time I only do these steps
Get changes.list
Shared Drive
Since then I've moved this folder into a Shared Drive/Team Drive where I added the service account with view permission to the whole shared drive.
After this change I can still list all files but the changes API always return an empty list of changes.
API Explorer/OAuth
So I tried to do the same using the API explorer using my user account/OAuth 2.0.
The result is the same, I can list files but I can't get any changes to appear.
I get a new startPageToken, response: 1207
Make changes in a google documents inside the Shared Drive
Request changes.list using the following
pageToken=1207
driveId=XXXXXXX
includeItemsFromAllDrives=true
spaces=drive
supportsAllDrives=true
The result is an empty array of changes and newStartPageToken being the same value as the pageToken I specified in the call.
{
"kind": "drive#changeList",
"newStartPageToken": "1207",
"changes": []
}
I've also tried without specifying driveId with the same results.
List files
The changed files that doesn't appear in the changes.list call can be found using the files.list call. Though this requires me to scan through all files every time I want to know what files changed.

DriveApp.remove(folder) using REST API?

I want to 'hide' a folder from the users Drive root, as it contains mostly junk Google Docs. I don't want to actually trash it, or use the appData storage as I can't then convert things to Google Docs.
In Apps Script, I can call DriveApp.remove(folder) and the file is given no parents at all, not even the root. I've tried calling the create method with "parents": [] but the folder is still created in the root.
Is there a way to make this happen with the REST API?
Figured it out. Supplying an empty parents list means Google will assign the folder to the root of your Drive. The only way to change this is to supply an update request indicating that you want to remove the parents of the folder that fall under the alias root.
In Python, it's as simple as this:
service.files().update(fileId=id, removeParents='root').execute()

Removed file is still accessible by collaborators in Google Drive. How I can delete it completely?

When I am deleting a file from Google drive which someone else shared with me, it showed me a message like "one removed file is still accessible by collaborators".
It no longer appears in that folder, but will it be shown in "shared with me"? How can I delete it completely?
tl;dr
To completely delete file in gdrive of owner of the removed file:
Search for is:unorganized owner:me
Delete file
Remove it from trash (delete it permanently)
Explanation:
This is a common problem with Google Drive. Files removed from the Shared folder loses the location pointer and stays on the drive of the owner of the file with lost location property (cannot be accessible in standard way as we browse files in Google Drive).
The removed file can be found by searching for it by name or content, or using suggested the search phrase is:unorganized owner:me.
A scenario that commonly causes this problem and fix:
U (as you) and S(someone else) have shared folder with name Cats pictures
U add cat.jpg (and U is an owner of this file then)
S don't like this picture and use option Remove
The message removed file are still accessible by collaborators pops up
File cat.jpg is removed from shared folder Cats pictures
S is happy and never see a cat.jpg in Cats pictures folder
U cannot see this file either in Cats pictures folder but cat.jpg is still on U private Google Drive!
If U search in Google Drive for cat.jpg it find the file, and it location will be - (file lost it's location in folder tree).
The file found by U can be moved to Trash and then completely deleted 🎈
You mentioned you'd deleted the file from Google drive which was shared by someone else with you. After deleting you saw a message that stated, "one removed file is still accessible by collaborators". Perhaps the one who is sharing the file is one of the mentioned collaborators, or depending on how this document was shared, other members of the group.
Hope this helps!
Rename file a number then delete twice or 3 times it won't recognize it I had 6and there gone now

How can I get a list of all shared files on my google drive using one google drive sdk query?

In my iOS app, when I execute "queryForFilesList" with q="" I get a list of all files and folders on my Google Drive plus shared root folders, but not the files in the shared root folders. If I use Chrome to show shared folders and click on a shared folder, the files in that shared folder are shown correctly. If the owner subsequently adds another file to the folder he is sharing with me, that file then appears in the list of files obtained by the above query. So, the list that is returned seems to depend on "when" the file was shared rather than "if" the file was shared. I think this is a bug so I need a work around. Obviously Chrome is using the work around. Executing the query using Google's "try it now" produces the same result. I've tried using "children.list" with no luck; sharedWithMe with folderId=root returns nothing. Is there an alias for the shared root?
However, while composing this question, I think I discovered a work around. If you use the id of the shared root folder (say 'xyz') and execute a "queryForFilesList" with q='xyz' in parents, all the files in the folder with id=xyz are returned as expected.
sharedWithMe query will not return the files under the shared folders, it only returns individually shared files and folders.
You can additionally query for '<sharedFoldersId>' in parents to list all of the files under a shared folder.
It's not the best practice, but you can use a following query in list reuest to list most of the shared to you files including children and shared by links files and folders, that were viewed by You.
https://www.googleapis.com/drive/v2/files?q=not+'{your_email_address}'+in+owners