Created
February 21, 2017 12:01
-
-
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#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
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
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.