Pergunta

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

Foi útil?

Solução

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.

Outras dicas

This blog post explains step by step how to implement a 2 legged authentication with the Google API .Net client.

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

However, the author ends his post by this comment:

The above information specific to the google-api-dotnet-client project is relevant, but that library leaks memory like a sieve and will kill your performance if you try to do any asynchronous work or use 2LO (on behalf of multiple users).

Good luck and let me know if you figured out a better solution, I'm a bit sick of the issue...

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 that lists all Analytics accounts, considering that GOOGLE_APPS_DOMAIN_SECRET is the key generate using this method, the DOMAIN is the domain of the Google Apps, and username is the prefix (ex. : "firstname.lastname") of the email from the Google Apps user we want to impersonate (the admin for example, 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 a user, and cannot simply have access to everything.

EDIT: Method deprecated

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top