title: Google .Net Service Account access to Calendar
body: <p>I am updating a web service application that calls Google's calendar API's to list calendar events for a particular calendar and insert new calendar events.  I am trying to upgrade it to version 3 of the api's.  For authentication I am using a Service Account Credential that I created  in the Google Developers Console (<a href="https://console.developers.google.com" rel="nofollow">https://console.developers.google.com</a>).  I am able to create the CalendarService using the following code :</p>
  
  <pre><code>using System;
  using Google.Apis.Auth.OAuth2;
  using System.Security.Cryptography.X509Certificates;
  using Google.Apis.Services;
  using Google.Apis.Calendar.v3;
  using Google.Apis.Calendar.v3.Data;
  
  ...
  
          string SERVICE_ACCOUNT_EMAIL = 
           "....googleusercontent.com";
          string SERVICE_ACCOUNT_PKCS12_FILE_PATH = @"C:\temp\API Project-123456789.p12";
  
          // Create the service.
  
          X509Certificate2 certificate = new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret", X509KeyStorageFlags.Exportable);
  
          ServiceAccountCredential credential = new ServiceAccountCredential(
                     new ServiceAccountCredential.Initializer(SERVICE_ACCOUNT_EMAIL)
                     {
                         Scopes = new[] { CalendarService.Scope.Calendar }
                        , User = "something@mycompany.com"
  
                     }.FromCertificate(certificate));
  
          // Create the service.
          var cs = new CalendarService(new BaseClientService.Initializer()
          {
              HttpClientInitializer = credential,
              ApplicationName = "Calendar API Sample",
          });   
  </code></pre>
  
  <p>But when I call the list method to query a public calendar:</p>
  
  <p>Events events = service.Events.List("something@mycompany.com").Execute();</p>
  
  <p>A TokenResponseException is thrown with the following error message:</p>
  
  <p>Error:"invalid_grant", Description:"", Uri:""</p>
  
  <p>FYI : I have gone into the AdminHome for my company and under security Manage clients API Access and registered  the SERVICE_ACCOUNT_EMAIL above to  <a href="http://www.google.com/calendar/feeds/" rel="nofollow">http://www.google.com/calendar/feeds/</a>.</p>
  
  <p>Any help with this would be greatly appreciated.</p>