How do I use the Google Analytics API with two-way OAuth (Google Apps for Business)?

I want to develop a business application in which I work. We use Google Apps and want to receive data from Google Analytics and display it in one of our web applications. I do not want the client to see any request to authorize the application. I want to use OAuth with two legs, for example http://www.google.com/support/a/bin/answer.py?hl=en&answer=162105 , but Google Analytics is not listed. Will I be able to use it? Is it supported in the Google Data API .NET or Google .NET Client API ?

EDIT 1 :

Using the Google API.NET Client , I came up with something that should work in my sense:

var auth = new Google.Apis.Authentication.OAuth2LeggedAuthenticator(DOMAIN_CONSUMER_KEY, DOMAIN_CONSUMER_SECRET, USER_TO_IMPERSONATE, DOMAIN); var service = new Google.Apis.Analytics.v3.AnalyticsService(auth); service.Key = DEV_KEY_FROM_API_CONSOLE; var _request = service.Management.Accounts.List(); foreach (var item in _request.Fetch().Items) { Console.WriteLine(item.Name); } 

... but I get this error:

 Google.Apis.Requests.RequestError InvalidCredentials [401] Errors [ Message[Invalid Credentials] Location[Authorization - header] Reason[authError] Domain[global] ] 

thanks

+4
source share
3 answers

I answered a similar question here . This will help solve several problems that will cause 401 Invalid Credentials and help you get the version of .Net APIs. I just add this here, since your solution uses the deprecated API v2 to get around the problem you encountered during authentication.

+1
source

This blog post explains step by step how to implement two-legged authentication with an API.Net client.

http://bittwiddlers.org/?p=212#awp::?p=212

However, the author ends his post with the following comment:

The above information related to the google-api-dotnet-client project does matter, but this library loses memory like a sieve and will kill your performance if you try to do any asynchronous work or use 2LO (on behalf of several users).

Good luck and let me know if you came up with a better solution, I'm a little tired ...

+3
source

I found a method using . NET library for the Google Data API . I found this link thanks to this guy . Here is a working code listing all Google Analytics accounts, given that GOOGLE_APPS_DOMAIN_SECRET is the key generated using this method , the DOMAIN domain is the Google Apps domain, and the username is the prefix (for example: "firstname.lastname") of the letter from The Google Apps user to whom we want to impersonate (for example, an administrator to have access to everything):

  var requestFactory = new GOAuthRequestFactory("an", "Any app name"); requestFactory.ConsumerKey = DOMAIN; requestFactory.ConsumerSecret = GOOGLE_APPS_DOMAIN_SECRET; var service = new AnalyticsService("Any app name"); service.RequestFactory = requestFactory; var query = new AccountQuery(); query.OAuthRequestorId = username + "@" + DOMAIN; var serviceQuery = service.Query(query); foreach (AccountEntry Entry in serviceQuery.Entries) { WriteLine(Entry.Title.Text); } 

The only problem is that I have to impersonate the user and cannot just access everything.

EDIT: method deprecated

0
source

All Articles