How to get Google Analytics data using OAuth?
Asked Answered
A

4

9

Hy guys, we are developing a system which will provide users with access to Google Analytics. I'm trying to implement it in the way so user don't need to enter their Google login credentials on our site, so trying to get it work using their login.

I have a solution which gets analytics using user's email and password. I'm looking for a solution which will not require user's email and password but can not find anything.

How can it be done? any advices or links will be appreciated.

thanks

Arturo answered 15/2, 2011 at 15:31 Comment(1)
Analytics can also be shared to specific user (on the basis of email)Demeter
A
11

Ok, guys, after a few days of struggle I finally figured this out. There is no documentation on the Internet and people who had done it before did not want to share their success by some reason. I found this discussion which helped me.

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

so

using DotNetOpenAuth.ApplicationBlock;
using DotNetOpenAuth.OAuth;

using Google.GData.Client;
using Google.GData.Analytics;

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);

This service you can use like here or here

And the last thing, you WILL NOT be available to try and test it on localhost so you will need a domain which MUST be registered with Google here in order to get consumer key and secret

Arturo answered 1/3, 2011 at 10:15 Comment(1)
I downloaded all these libraries and there is no such member as service.RequestFactory = requestFactory;. Also, do you know by chance how to use Analytics class in namespace Google.Apis.Analytics.v3Bloem
H
1

There's a .NET/C# class for Google Data authentication that can be used to access the Google Analytics Data Export API (since the API is part of the Google Data standard, though you might need to make Google Analytics specific adjustments.)*

The authentication is best managed by creating a Google Registered Application, as this allows you to make the authentication without security warnings (and, for that matter, security lapses).

There are three forms of supported authentication; the 'secure'/passwordless ones are OAuth and AuthSub (which is the Google-proprietary version of OAuth); the hardcoded username and password version is referred to by Google as 'ClientLogin', and is not considered secure or ideal for multiple-user applications.

*(Since you tagged the question )

Edit: More details on using AuthSub or OAuth with the .NET library:

AuthSubSupport: http://code.google.com/p/google-gdata/wiki/AuthSubSupport

Code Samples on how to use the libraries for OAuth authentication: http://code.google.com/apis/gdata/docs/auth/oauth.html#2LeggedOAuth (Click the .NET tab).

Helsa answered 15/2, 2011 at 16:59 Comment(2)
link to code.google.com/apis/gdata/client-cs.html does not provide any reasonable examle, shown one using ClientLogin using email and password which is inacceptable. Google documentation does not help at all. I have no idea how to generate all those huge cumbersome requests manually and then parse responces. This task seems to my me hopeless for one who has not many years of experience.Arturo
@Arturo See my 2 edits. They should help. I know what you're going through -- I had to do this for PHP, and its not simple to get working. But the guides are helpful.Helsa
V
0

Basics of working with OAuth are here: http://code.google.com/apis/accounts/docs/OpenID.html#working

Authenticating with OAuth: http://code.google.com/apis/accounts/docs/OAuth.html

After you authenticate a user with OAuth, you will have the request token that works like the one you get back from Google's login API. From there, it should be the same as username/password.

Vogeley answered 15/2, 2011 at 15:46 Comment(0)
H
-1

I don't think you need to mess with OAuth.

The google analytics api lets you pass credentials. Just start from this data feed example.

http://code.google.com/p/google-gdata/source/browse/trunk/clients/cs/samples/Analytics_DataFeed_Sample/dataFeed.cs

// Configure GA API and do client login Authorization.
AnalyticsService asv = new AnalyticsService("gaExportAPI_acctSample_v2.0");
asv.setUserCredentials(clientUser, clientPass);

Download the client library here

http://code.google.com/apis/analytics/docs/gdata/gdataLibraries.html

To get a feel for data queries, play with this and then copy the values into the example above

http://code.google.com/apis/analytics/docs/gdata/gdataExplorer.html
Haik answered 20/10, 2011 at 17:19 Comment(1)
This is mor suited to development only. If you deploy this in production besides the security risks you get capped at 200 requests a dayAlexaalexander

© 2022 - 2024 — McMap. All rights reserved.