Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use SpreadsheetsService authenticated by ServiceAccountCredential?

I need to use SpreadsheetsService, but I don't find the way in the official documentation .net. https://developers.google.com/google-apps/spreadsheets/?hl=ja#authorizing_requests

I have my service authenticated:

String serviceAccountEmail = "[email protected]";

var certificate = new X509Certificate2(@"privatekey.p12", "pass", X509KeyStorageFlags.Exportable);

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

From here I can instantiate almost any service.

For example Drive Service:

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

But with SpreadsheetsService I can do this, because SpreadsheetsService wait for an string 'application name' in his default constructor or an GOAuth2RequestFactory in his property RequestFactory.

How to authenticate SpreadsheetsService with an ServiceAccountCredential?

like image 812
Erick Asto Oblitas Avatar asked Jun 12 '14 13:06

Erick Asto Oblitas


People also ask

How do I access Google Spreadsheets with a service account credentials?

To access the data stored in Google Sheets, you will need to create a service account and get a set of OAuth2 credentials from the Google API Console. Access the Google APIs Console while logged into your Google account. Create a new project and give it a name. Click on ENABLE APIS AND SERVICES .

How do I get a Google spreadsheet token?

When your application needs access to user data, it asks Google for a particular scope of access. Google displays a consent screen to the user, asking them to authorize your application to request some of their data. If the user approves, then Google gives your application a short-lived access token.


1 Answers

Here is the answer on how to do this..... The key is to use the customHeaders on a new requestFactory

 var certificate = new
 System.Security.Cryptography.X509Certificates.X509Certificate2(p12KeyFileName,
 "notasecret", X509KeyStorageFlags.Exportable);

 string SCOPE = "https://spreadsheets.google.com/feeds
 https://docs.google.com/feeds";

 Google.Apis.Auth.OAuth2.ServiceAccountCredential credential = new
 Google.Apis.Auth.OAuth2.ServiceAccountCredential(
     new ServiceAccountCredential.Initializer(serviceAccountEmail)
     {
         Scopes = new[] { SCOPE }
     }.FromCertificate(certificate));


 bool success =
 credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Result;

 var requestFactory = new Google.GData.Client.GDataRequestFactory("My
 Plastic SCM Service");
 requestFactory.CustomHeaders.Add(string.Format("Authorization: Bearer
 {0}", credential.Token.AccessToken));

 var s = new
 SpreadsheetsService("MySpreadsheetIntegration-v1"); s.RequestFactory =
 requestFactory; 
 return s;
like image 182
JeremyS Avatar answered Oct 27 '22 03:10

JeremyS