title: Google Analytics API 3 - Error:"invalid_grant", Description:"", Uri:""
body: <p>I've 'googled' the life out of this issue today with zero resolve!</p>
  
  <p>I am trying to build a very simple Google Analytics data request console app using a Service account. I have set up all the required details in the Google Developers Console but I am getting the following error:</p>
  
  <pre><code>An unhandled exception of type 'Google.Apis.Auth.OAuth2.Responses.TokenResponseException'          
  occurred in Google.Apis.dll
  
  Additional information: Error:"invalid_grant", Description:"", Uri:""
  </code></pre>
  
  <p>Below is the code in my console app (with keys hidden):</p>
  
  <pre><code>using System.Security.Cryptography.X509Certificates;
  using Google.Apis.Auth.OAuth2;
  using Google.Apis.Services;
  using Google.Apis.Analytics;
  using Google.Apis.Analytics.v3;
  using Google.Apis.Analytics.v3.Data;
  using System;
  
  namespace GoogleAnalyticsAPI
  {
  
  public class Program
  {
  
      public static void Main(string[] args)
      {
  
          string profileId = "12345678";
          string serviceAccountEmail = "123456789abcdefghijklmnopq@developer.gserviceaccount.com";
          X509Certificate2 certificate = new X509Certificate2(@"PrivateKey.p12", "mypassword", X509KeyStorageFlags.Exportable);
  
          // Create credentials
          ServiceAccountCredential credential = new ServiceAccountCredential(
             new ServiceAccountCredential.Initializer(serviceAccountEmail)
             {
                 Scopes = new[] { AnalyticsService.Scope.Analytics }
             }.FromCertificate(certificate));
  
          // Create the service
          var service = new AnalyticsService(new BaseClientService.Initializer()
          {
              HttpClientInitializer = credential,
              ApplicationName = "Google Analytics API"
  
          });
  
          string startDate = "2014-07-01";
          string endDate = "2010-07-31";
  
  
          DataResource.GaResource.GetRequest request = service.Data.Ga.Get(string.Format("ga:{0}", profileId), startDate, endDate, "ga:visits, ga:newVisits");
          request.Dimensions = "ga:city";
          GaData data = request.Execute();
  
      }
  
  }
  }
  </code></pre>
  
  <p>Any help here would be greatly appreciated!</p>
  
  <p>Note, here are some sources I've followed:</p>
  
  <p><a href="http://www.toplinestrategies.com/dotneters/net/accessing-and-querying-google-analytics-via-service-account-c/" rel="nofollow">Tutorial Followed to get above code</a></p>
  
  <p><strike><a href="http://www.daimto.com/google-analytics-api-v3-with-c/" rel="nofollow">Useful but out of date</a></strike></p>
  
  <p><a href="https://developers.google.com/analytics/devguides/reporting/core/v3/coreDevguide" rel="nofollow">Google's code walk through - not in C# .Net</a></p>