3

I've 'googled' the life out of this issue today with zero resolve!

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:

An unhandled exception of type 'Google.Apis.Auth.OAuth2.Responses.TokenResponseException'          
occurred in Google.Apis.dll

Additional information: Error:"invalid_grant", Description:"", Uri:""

Below is the code in my console app (with keys hidden):

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 = "[email protected]";
        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();

    }

}
}

Any help here would be greatly appreciated!

Note, here are some sources I've followed:

Tutorial Followed to get above code

Useful but out of date

Google's code walk through - not in C# .Net

3
  • What does this have to do with the Google Maps Javascript API v3?
    – geocodezip
    Aug 19, 2014 at 15:31
  • I didn't even realise that was tagged! Cheers I'll remove it. Aug 19, 2014 at 15:40
  • 1
    In addition to the answer below, make sure that the system clock is set to the right time. See stackoverflow.com/questions/11973162/… Mar 26, 2015 at 17:23

1 Answer 1

10

It turns out I had the wrong credentials, if anyone else has the same problem:

  1. Double check you're using the service account email ending "@developer.gserviceaccount.com"
  2. Check the downloaded .p12 file is located in your debug folder and you supply the correct private key password (given to you when you create your service account).
3
  • 1
    I'm having the same problem. Already checked the service account email, the .P12 certificate and the standard password "nodatasecret" provided by Google. When I try to get Calendar items with IList<CalendarListEntry> calendarList = service.CalendarList.List().Execute().Items; I'm still getting the error: Error:"invalid_grant", Description:"", Uri:"". Any idea? Mar 2, 2015 at 8:49
  • Maybe this answer will help: Link Mar 2, 2015 at 8:55
  • Yeah, I had the same problem.
    – Greg
    Jul 29, 2015 at 21:09

Your Answer

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

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