Karthik Ramesh
Karthik Ramesh

Reputation: 17

C# Google Drive API Error " Google.Apis.Services.BaseClientService.Initializer' does not contain a definition for 'Authenticator' "

I am trying a sample code with domain wide delegation of authority. I am implementing the code specified in this link: https://developers.google.com/drive/web/delegation

    private DriveService GetService(String userEmail)
    {

        X509Certificate2 certificate = new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret", X509KeyStorageFlags.Exportable);

        var provider = new AssertionFlowClient(GoogleAuthenticationServer.Description, certificate)
        {
            ServiceAccountId = SERVICE_ACCOUNT_EMAIL,
            Scope = DriveService.Scope.Drive.GetStringValue(),
            ServiceAccountUser = userEmail,
        };

        authenticator = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState);


        return new DriveService(new BaseClientService.Initializer() { Authenticator = authenticator, ApplicationName = "sample" });

    }

When i compile it i am getting an error saying "'Google.Apis.Services.BaseClientService.Initializer' does not contain a definition for 'Authenticator' ". i think its because of a recent change to the API. So can someone please suggest as to how to overcome this error?

Upvotes: 0

Views: 3155

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116938

I would like to recomend you use the Google dot net client library.

NuGet command

PM> Install-Package Google.Apis.Drive.v2 

Sample code:

using Google.Apis.Auth.OAuth2;
using System.Security.Cryptography.X509Certificates;
using Google.Apis.Services;
using Google.Apis.Drive.v2;  

var serviceAccountEmail = "539621478859-imkdv94bgujcom228h3ea33kmkoefhil@developer.gserviceaccount.com";
ServiceAccountCredential certificate = new X509Certificate2(@"C:\dev\GoogleDriveServiceAccount\key.p12", "notasecret", X509KeyStorageFlags.Exportable);

ServiceAccountCredential credential = new ServiceAccountCredential(
           new ServiceAccountCredential.Initializer(serviceAccountEmail)
           {
               Scopes = new[] { DriveService.Scope.DriveReadonly }
           }.FromCertificate(certificate));

// Create the service.
var service = new DriveService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "Drive API Sample",
        });

All your requests are then run though service. If you din't already make sure that you have both the Drive API and Drive SDK selected in your developer console application application.

Upvotes: 1

Related Questions