Reputation: 75
I initialized the Google API Service and passed the service to Create folder method, at each time it creates the same folder in Google Drive, I wanna apply the logic If folder exist, don't create it, just return its FolderID else create the new folder and return its folder id. Please suggest on it.
var service= InitializeGoogleDriveAPIService(clientId, clientSecret);
var res=CreateFolder(service, "FolderName");
private static DriveService InitializeGoogleDriveAPIService(string clientId, string clientSecret)
{
string[] scopes = new string[] { DriveService.Scope.Drive, DriveService.Scope.DriveFile, };
ClientSecrets clientSecrets = new ClientSecrets();
clientSecrets.ClientId = clientId;
clientSecrets.ClientSecret = clientSecret;
// Below is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(clientSecrets, scopes, Environment.UserName, CancellationToken.None, new FileDataStore("MyAppsToken")).Result;
//Once consent is received, the token will be stored locally on the AppData directory,
//so that next time we won't be prompted for consent. -one time setting for the Console app
BaseClientService.Initializer initializer = new BaseClientService.Initializer();
initializer.HttpClientInitializer = credential;
initializer.ApplicationName = "PRInvoices";
DriveService service = new DriveService(initializer);
service.HttpClient.Timeout = TimeSpan.FromMinutes(100);
//Long Operations like file uploads might timeout. 100 is just precautionary value,
//can be set to any reasonable value depending on what you use your service for
return service;
}
public static string CreateFolder(DriveService service, string folderName)
{
var file = new Google.Apis.Drive.v3.Data.File { Name = folderName, MimeType = "application/vnd.google-apps.folder" };
// var RESILT = service.Files.List();
var result = service.Files.Create(file).Execute();
return result.Id;
}
Upvotes: 1
Views: 2684
Reputation: 2998
Since files names are not unique in google drive we will have to find the filename to determine if it exists or not. Here a working example:
public static string CreateFolder(DriveService service, string folderName)
{
var newFile = new Google.Apis.Drive.v3.Data.File { Name = folderName, MimeType = "application/vnd.google-apps.folder" };
// Define parameters of request.
FilesResource.ListRequest listRequest = service.Files.List();
listRequest.PageSize = 10;
listRequest.Fields = "nextPageToken, files(id, name)";
// List files.
IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute()
.Files;
if (files != null && files.Count > 0)
{
foreach (var file in files)
{
if(file.Name == newFile.Name) {
Console.WriteLine("File already existing... Skip creation");
return file.Id;
}
}
}
else
{
Console.WriteLine("No files found.");
}
Console.WriteLine("Creating new file...");
var result = service.Files.Create(newFile).Execute();
return result.Id;
}
Upvotes: 2