title: Unable to authenticate Google Calendar API v3 programmatically?
body: <p>Perhaps I am the only one that thinks Google's API documentation is awful but I've spent more time on this simple task than I wanted. </p>
  
  <p>Currently my project is using a GDATA implementation to connect with the Google Calendar API v2. I followed this guide: <a href="http://www.codeproject.com/Articles/565032/Google-Calendar-Integration-in-ASP-NET-Create-ed" rel="nofollow">http://www.codeproject.com/Articles/565032/Google-Calendar-Integration-in-ASP-NET-Create-ed</a></p>
  
  <p>But I noticed that Google is deprecating version 2 of their API this fall. I am trying to figure out how I can connect to their version 3 API which appears to be using OAuth2. </p>
  
  <p>After reading their documentation and searching the internet >:( - The problem I keep running into is EVERY sample, tutorial or youtube video I've come across that shows how to implement this involve the Google consent screen where the user clicks "Accept".</p>
  
  <p>I've tried doing the following but honestly not sure if it's even the right direction?</p>
  
  <pre><code>    // Register the authenticator. The Client ID and secret have to be copied from the API Access
      // tab on the Google APIs Console.
      var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);
      provider.ClientIdentifier = "MY_CLIENT_ID";
      provider.ClientSecret = "MY_CLIENT_SECRET";
  
      // Create the service. This will automatically call the previously registered authenticator.
      var service = new CalendarService();
  </code></pre>
  
  <p>My application doesn't need the user's account/consent (OAuth), I need to connect like I am currently in my code-behind.</p>
  
  <p>So the question is how do I "upgrade" my current implementation to v3? Do I use OAuth, Service Account? I've found plenty of examples showing the v3 usages for how to retrieve events and insert them... but they all authenticate with a user consent screen on the front end.</p>
  
  <p>Here is my current GData implementation...</p>
  
  <pre><code>public class GoogleGateway : IGoogleGateway
      {
          private readonly IRepository&lt;UserSetting&gt; _settingsRepository;
          private Service _googleService;
          private CalendarService _googleCalendar;
          private Uri _calendarUri;
  
          public GoogleGateway(IRepository&lt;UserSetting&gt; settingsRepository)
          {
              _settingsRepository = settingsRepository;
          }
  
          public IEnumerable&lt;EventEntry&gt; GetAllEvents(DateTime? startDate)
          {
              if (!Connect()) return new List&lt;EventEntry&gt;();
  
              // Create the query object:
              EventQuery query = new EventQuery();
              query.Uri = _calendarUri;
              if (startDate != null)
                  query.StartTime = startDate.Value;
  
              // Tell the service to query:
              EventFeed calFeed = _googleCalendar.Query(query);
              return calFeed.Entries.Cast&lt;EventEntry&gt;();
          }
  
          public bool Connect()
          {
              var calSettings = _settingsRepository.Get().Where(x =&gt; x.Setting == "Calendar");
  
              if (calSettings.Any())
              {
                  var username = calSettings.First(x =&gt; x.Meta == "GoogleUsername").Value;
                  var password = calSettings.First(x =&gt; x.Meta == "GooglePassword").Value;
                  var calendarUri = new Uri(calSettings.First(x =&gt; x.Meta == "CalendarFeed").Value);
                  var applicationName = calSettings.First(x =&gt; x.Meta == "ApplicationName").Value;
  
                  _calendarUri = calendarUri;
  
                  //FeedQuery feedQuery = new FeedQuery();
  
                  _googleService = new Service("cl", applicationName);
                  _googleCalendar = new CalendarService(applicationName);
  
                  // Set your credentials:
                  _googleService.setUserCredentials(username, password);
                  _googleCalendar.setUserCredentials(username, password);
  
                  return true;
              }
  
              return false;
          }
  
          public void AddEvent(string title, string contents, string location, DateTime startTime, DateTime endTime)
          {
              if (!Connect()) return;
  
              EventEntry.EVENT_CATEGORY = new AtomCategory("Appointments");
              EventEntry entry = new EventEntry
              {
                  Title = { Text = title },
                  Content = { Content = contents },
              };
  
              // Set the title and content of the entry.
  
              // Set a location for the event.
              Where eventLocation = new Where();
              eventLocation.ValueString = location;
              entry.Locations.Add(eventLocation);
  
              When eventTime = new When(startTime, endTime);
              entry.Times.Add(eventTime);
  
              Uri postUri = new Uri("http://www.google.com/calendar/feeds/default/private/full");
  
              // Send the request and receive the response:
              AtomEntry insertedEntry = _googleCalendar.Insert(postUri, entry);
          }
  
          public void DeleteEvent(string eventId)
          {
              if (!Connect()) return;
  
              var events = GetAllEvents(null);
  
              var appointment = events.First(x =&gt; x.EventId == eventId);
              _googleService.Delete(appointment);
          }
      }
  </code></pre>
  
  <p>I'm growing desperate at this point, any help would be very appreciated. Include your twitter handle in your answer and I'll buy you a coffee! </p>
  
  <p><strong>UPDATED</strong>
  I currently have the following, but I is still not authenticating... :(</p>
  
  <pre><code>    static CalendarService BuildService()
      {
          String serviceAccountEmail = "xxxxxxxxxxxxx-31xxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com";
  
          var certPath = HttpContext.Current.Server.MapPath("/xxxxxxxxxxxx.p12");
          var certificate = new X509Certificate2(certPath, "notasecret", X509KeyStorageFlags.Exportable);
  
          ServiceAccountCredential credential = new ServiceAccountCredential(
             new ServiceAccountCredential.Initializer(serviceAccountEmail)
             {
                 Scopes = new[] { CalendarService.Scope.Calendar }
             }.FromCertificate(certificate));
  
          // Create the service.
          var service = new CalendarService(new BaseClientService.Initializer()
          {
              HttpClientInitializer = credential, &lt;&lt;&lt;&lt;&lt;&lt; DOES NOT RESOLVE!
              ApplicationName = "MyApplication",
          });
  
          var test = service.Calendars.Get("xxxxxxxxxxxxxxxxxx@group.calendar.google.com");
  
          return service;
      }
  </code></pre>