Skip to content

Instantly share code, notes, and snippets.

@LindaLawton
Created February 21, 2017 12:01
Show Gist options
  • Save LindaLawton/6e3b76fa3e9a2d8d72d96fe7f1d71297 to your computer and use it in GitHub Desktop.
Save LindaLawton/6e3b76fa3e9a2d8d72d96fe7f1d71297 to your computer and use it in GitHub Desktop.
How to remove a file from the share with me folder on Google drive using the Google drive api V3 and C#
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
internal class GoogleDriveAuthentcation
{
/// <summary>
/// Authenticate to Google Using Oauth2
/// Documentation https://developers.google.com/accounts/docs/OAuth2
/// Credentials are stored in System.Environment.SpecialFolder.Personal
/// </summary>
/// <param name="clientId">From Google Developer console https://console.developers.google.com</param>
/// <param name="clientSecret">From Google Developer console https://console.developers.google.com</param>
/// <param name="userName">Identifying string for the user who is being authentcated.</param>
/// <returns>SheetsService used to make requests against the Sheets API</returns>
public static DriveService AuthenticateOauth(string clientId, string clientSecret, string userName)
{
try
{
if (string.IsNullOrEmpty(clientId))
throw new ArgumentNullException("clientId");
if (string.IsNullOrEmpty(clientSecret))
throw new ArgumentNullException("clientSecret");
if (string.IsNullOrEmpty(userName))
throw new ArgumentNullException("userName");
// These are the scopes of permissions you need. It is best to request only what you need and not all of them
string[] scopes = new string[] { DriveService.Scope.DriveReadonly }; //View the files in your Google Drive
var credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
credPath = System.IO.Path.Combine(credPath, ".credentials/apiName");
// Requesting Authentication or loading previously stored authentication for userName
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
, scopes
, userName
, CancellationToken.None
, new FileDataStore(credPath, true)).Result;
// Returning the SheetsService
return new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Drive Oauth2 Authentication Sample"
});
}
catch (Exception ex)
{
Console.WriteLine("Create Oauth2 account DriveService failed" + ex.Message);
throw new Exception("CreateServiceAccountDriveFailed", ex);
}
}
}
var service = GoogleDriveAuthentcation.AuthenticateOauth(clientId, secret, "My User");
// Get a list of files in the Share with me folder
var request = service.Files.List();
request.Q = "(sharedWithMe = true)";
request.Fields = "*";
var results = request.Execute();
// Find the file in question I wish to remove.
var myfile = results.Files.Where(a => a.Name.ToLower().Equals("receipt.pdf")).FirstOrDefault();
// Find the permissions for this user.
var per = myfile.Permissions.Where(a => a.EmailAddress.ToLower().Equals("[email protected]")).FirstOrDefault();
// Delete the permissions
service.Permissions.Delete(myfile.Id, per.Id).Execute();
@LindaLawton
Copy link
Author

Note this doesn't work if you don't have permissions added. I have a file shared with me by what looks like a service account. I have no permissions listed so cant delete it. I am still working on that.

@t-anh
Copy link

t-anh commented Jan 13, 2021

I have same problem with you.

Note this doesn't work if you don't have permissions added.

Do you have solution for this case?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment