6

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?

4
  • 1
    With Java you can use: spreadsheetService.setHeader("Authorization", "Bearer " + accessToken); But not sure about .net .... setHeader example: stackoverflow.com/questions/15084133/… Jun 17, 2014 at 0:09
  • Did you find the answer? I am trying to do the same thing, but SpreadsheetsService supports only application name as a parameter. Is there any workaround you used? setUserCredentials works for me if I am just a regular, not a domain user.
    – Sergei
    Aug 19, 2014 at 22:05
  • What's namespace and assembly for DriveService ? Aug 13, 2015 at 15:54
  • This might be helpful - blog.antontelle.com/post/…
    – Maz T
    Feb 23, 2017 at 10:33

2 Answers 2

6

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;
0

Note that when adding the custom header you need: "Authorization: Bearer {0}" <-- with a space between "Bearer" and "{0}" Because: "Authorization: Bearer{0}" <-- no space will only get you an "unknown authorization header" Error 401 response

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.