Search code examples
.netoauthgoogle-analyticsgoogle-api2-legged

How to use Google Analytics API with 2-legged OAuth (Google Apps for business)?


I want to develop an application for the business I work. We are using Google Apps and want to get data from Google Analytics and show it in one of our web apps. I do not want the client to see any request to authorize the app. I want to use 2-legged OAuth like such http://www.google.com/support/a/bin/answer.py?hl=en&answer=162105 but Google Analytics is not in the list. Will I still be able to use it ? Is it supported in the .NET Google Data API library or the Google API .NET Client ?

EDIT 1 :

Using the Google API .NET Client, I came up with something that should work to 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


Solution

  • I have answered a similar question here. That answers how to fix a couple of problems that will cause 401 Invalid Credentials and may help you get the v3 version of the .Net APIs working. I'm just adding this here as your solution uses the deprecated v2 API to circumvent the problem you were having with authentication.