Google API to upload files using the Authentication Service Account

58 views
Skip to first unread message

Carlos Rogerio

unread,
Feb 26, 2015, 3:21:42 PM2/26/15
to google-api-d...@googlegroups.com
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-6s4s95s9u...@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.

Linda Lawton

unread,
Apr 20, 2015, 3:44:02 PM4/20/15
to google-api-d...@googlegroups.com
Um where exactly did you download this from?

daimto.com or https://github.com/LindaLawton/Google-Dotnet-Samples/tree/master/Google-Drive

It looks like you have either merged two tutorials google+ and google drive or I have messed up some sample code someplace. I am going to start searching the website and sample projects see if i cant find it.


Btw daimto is a she :)
Reply all
Reply to author
Forward
0 new messages