I'm trying to send files by API Google Drive, though, I can not find any documentation on how to perform C # Uploading files using the Authentication Service Account.
I downloaded the Daimto library, however, he uploads using the DriveService class, when we use the authentication ClientId and ClientSecret. But using authentication for account service he returns to PlusService class, and found no way to upload files this way.
Can someone help me? Best regards
Using Authentication Service Account
public PlusService GoogleAuthenticationServiceAccount()
{
String serviceAccountEmail = "106842951064-6s4s95s9u62760louquqo9gu70ia3ev2@developer.gserviceaccount.com";
//var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.Exportable);
var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { PlusService.Scope.PlusMe }
}.FromCertificate(certificate));
// Create the service.
var service = new PlusService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Plus API Sample",
});
return service;
}
Using Authentication ClientId and ClientSecret
public DriveService GoogleAuthentication(string userClientId, string userSecret)
{
//Scopes for use with the Google Drive API
string[] scopes = new string[] { DriveService.Scope.Drive, DriveService.Scope.DriveFile };
// here 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(new ClientSecrets { ClientId = userClientId, ClientSecret = userSecret }, scopes, Environment.UserName, CancellationToken.None, new FileDataStore("Daimto.GoogleDrive.Auth.Store")).Result;
DriveService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Drive API Sample"
});
return service;
}
Daimto class method that makes the file upload to google drive DaimtoGoogleDriveHelper.uploadFile(_service, fileName, item.NomeArquivo, directoryId);
As you can see, the Daimto library, has a method to upload, however, uses the _service parameter, which is the DriveService type, which is what is returned by GoogleAuthentication method. But the GoogleAuthenticationServiceAccount method returns a PlusService type and is incompatible with the DriveService type.
I am not sure which one of my tutorials you are following. But your first hunk of code is using a PlusService you should be using a DriveService. Any requests you make to the Google drive API must go though a DriveService
Authenticating to Google drive with a service account:
Upload a file:
The upload code is the same if you are using Oauth2 or a service account there is no difference.
Code ripped from Google Drive .net sample on Github Tutorial: Google Drive API with C# .net – Download