0

I am trying to write a service that will pull Google Analytics data via a windows service or a console app.

No matter what I try, I am unable to authorize my app using oAuth

I can do something like this

 var analyticsService = new AnalyticsService("MyApp");
 const string baseUrl = "https://www.google.com/analytics/feeds/data";

 var dataQuery = new DataQuery(baseUrl);

 dataQuery.Ids = TableId;
 dataQuery.Dimensions = "ga:pagePath,ga:date";
 dataQuery.Metrics = "ga:avgTimeOnPage,ga:pageviews,ga:uniquePageviews";
 dataQuery.Sort = "ga:date";
 dataQuery.GAStartDate = "2012-03-01";
 dataQuery.GAEndDate = "2012-04-15";

 Feed = analyticsService.Query(dataQuery);

This works fine if I use GDataCredentials with my account username/pw. I was under the impression that this only gives me 200 requests a day. I really need help with some example code how to make it work with oAuth to allow for 50K requests. I am totally hitting the wall here.

Just to Clarify:

I have a single GoogleAnalytics account for my site. I will always be pulling from that one account. What is the easiest way to do it, without hitting the low API limits?

Thanks a lot in advance!

Greg R
  • 1,670
  • 4
  • 26
  • 46

2 Answers2

1

After further research, it looks like the option that I'm looking for is Service Accounts, which is not available (yet?) for Google Analytics

https://developers.google.com/accounts/docs/OAuth2ServiceAccount

Greg R
  • 1,670
  • 4
  • 26
  • 46
0

Answer could be found here.

Quotes from original author:

To make it work you will need DotNetOpenAuth from http://www.dotnetopenauth.net/ and gdata from http://code.google.com/p/google-gdata/

In DotNetOpenAuth there is sample project named OAuthConsumer which you need. Change it to requiest authorization for Analytics:

GoogleConsumer.RequestAuthorization(google, GoogleConsumer.Applications.Analytics);

This will get you Token and Token secret. You can use them like this:

    GOAuthRequestFactory requestFactory = new GOAuthRequestFactory("cp", TokenManager.ConsumerKey); //ConsumerKey actually is the name of web application
    requestFactory.ConsumerKey = TokenManager.ConsumerKey;
    requestFactory.ConsumerSecret = TokenManager.ConsumerSecret;
    requestFactory.Token = AccessToken;
    requestFactory.TokenSecret = TokenManager.GetTokenSecret(AccessToken);
    requestFactory.UseSSL = true;
    AnalyticsService service = new AnalyticsService(requestFactory.ApplicationName); // acually the same as ConsumerKey
    service.RequestFactory = requestFactory;

    const string dataFeedUrl = "https://www.google.com/analytics/feeds/data";

    DataQuery query1 = new DataQuery(dataFeedUrl);
Community
  • 1
  • 1
Pavel Krymets
  • 6,253
  • 1
  • 22
  • 35
  • Right, my problem is that I tried it and it doesn't work :) It's missing all sorts of references, the APIs have changed, version of libraries and so forth. Could you provide a large sample? – Greg R Apr 16 '12 at 14:08