5

I'm trying to write an online application to access my google analytics data using a google service account. Here's my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace GA_server2server_POC.Models
{
    using System.Security.Cryptography.X509Certificates;
    using Google.Apis.Analytics.v3;
    using Google.Apis.Analytics.v3.Data;
    using Google.Apis.Authentication.OAuth2;
    using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
    using Google.Apis.Util;
    using Google.Apis.Services;
    using Google.Apis.Requests;

    public class Oauth_With_API
    {
        public static void ApiTest()
        {
            log4net.Config.XmlConfigurator.Configure();
            const string ServiceAccountId = "xxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com";
            const string ServiceAccountUser = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxdeveloper.gserviceaccount.com";
            AssertionFlowClient client = new AssertionFlowClient(
                GoogleAuthenticationServer.Description, new X509Certificate2("C:\\Users\\rcarter\\Downloads\\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-privatekey.p12", "notasecret", X509KeyStorageFlags.Exportable))
            {
                Scope = "https://www.googleapis.com/auth/analytics.readonly",
                ServiceAccountId = ServiceAccountUser
            };



            OAuth2Authenticator<AssertionFlowClient> authenticator = new OAuth2Authenticator<AssertionFlowClient>(client, AssertionFlowClient.GetState);

            AnalyticsService service = new AnalyticsService(new BaseClientService.Initializer()
            {
                Authenticator = authenticator
            });

            string profileId = "ga:xxxxxxxx";
            string startDate = "2013-07-01";
            string endDate = "2013-07-15";
            string metrics = "ga:visits";
            DataResource.GaResource.GetRequest request = service.Data.Ga.Get(profileId, startDate, endDate, metrics);
            request.Dimensions = "ga:date";
            GaData data = request.Execute(); //error occurs here. After this, thread exits.

            Console.WriteLine(data.TotalResults);

        }
    }
}

So far my code executes, but I get the following output:

WebDev.WebServer40.exe Information: 0 : DotNetOpenAuth, Version=4.0.0.11165, Culture=neutral, PublicKeyToken=2780ccd10d57b246 (official)
WebDev.WebServer40.exe Information: 0 : Preparing to send AssertionFlowMessage (2.0) message.
WebDev.WebServer40.exe Information: 0 : Sending AssertionFlowMessage request.
WebDev.WebServer40.exe Information: 0 : HTTP POST https://accounts.google.com/o/oauth2/token
WebDev.WebServer40.exe Information: 0 : The following required parameters were missing from the DotNetOpenAuth.OAuth2.Messages.AccessTokenFailedResponse message: {error,
}
WebDev.WebServer40.exe Information: 0 : Received UnauthorizedResponse response.

After this, the thread exits, and the program refuses to print any of the data. The trouble seems to occur at request.Execute();. The part that I find especially confusing, is that if I put a breakpoint on Console.WriteLine(data.TotalResults);, I can see the data I want in the local variable data. It contains everything I want to print, but I can't identify the cause of the error keeping it from doing anything after request.Execute();. After much searching, I haven't found much at all about the error listed above.

The code I'm using is based on the answer given to this question here. A few things have changed in the google analytics libraries since that question was answered, but much of my code is the same.

I've checked and re-checked all the account-specific variables. To test this, I'm running it on my local machine as a ASP.NET MVC 4 Web App.

Any help or advice on how to troubleshoot this issue is appreciated. Please let me know if I can provide more information which might help. Thanks for reading.

Community
  • 1
  • 1
  • It is quite easy.Problem is there is no much documentation/samples are available there. I even have converted some java code to .net equivalent as the sample were only in java. I am sharing some code i have available locally at my other system.Let me know if it helps or not.Other wise i will send you full working sample Thanks – Kamran Shahid Jul 18 '13 at 19:37

1 Answers1

0

Try following one

using System.Security.Cryptography.X509Certificates;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Analytics.v3;
using Google.Apis.Analytics.v3.Data;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Services;


        private void TestMethod()
        {
            try
            {
                string scope_url = "https://www.googleapis.com/auth/analytics.readonly";

                //client_id: This is the "Email Address" one, not the "Client ID" one... oddly...
                string client_id = "************-***********************@developer.gserviceaccount.com";

                //key_file: This is the physical path to the key file you downloaded when you created your Service Account
                string key_file = @"***************************************-privatekey.p12";

                //key_pass: This is probably the password for all key files, but if you're given a different one, use that.
                string key_pass = "notasecret";


                AuthorizationServerDescription desc = GoogleAuthenticationServer.Description;

                //key: Load up and decrypt the key
                X509Certificate2 key = new X509Certificate2(key_file, key_pass, X509KeyStorageFlags.Exportable);

                //client: we're using the AssertionFlowClient, because we're logging in with our certificate
                AssertionFlowClient client = new AssertionFlowClient(desc, key) { ServiceAccountId = client_id, Scope = scope_url };
                OAuth2Authenticator<AssertionFlowClient> auth = new OAuth2Authenticator<AssertionFlowClient>(client, AssertionFlowClient.GetState);

                //gas: An instance of the AnalyticsService we can query
                // AnalyticsService gas = null;// new AnalyticsService(auth);

                var gas = new AnalyticsService(new BaseClientService.Initializer()
                {
                    Authenticator = auth
                });
                //r: Creating our query
                DataResource.GaResource.GetRequest r = gas.Data.Ga.Get("ga:*******", "2012-09-26", "2012-10-10", "ga:visitors");

                //d: Execute and fetch the results of our query
                GaData d = r.Fetch();
            }
            catch (Exception ex)
            {

                throw;
            }
        }
Kamran Shahid
  • 3,954
  • 5
  • 48
  • 93