7

I'm trying to use the latest .NET client libraries to access Version 3.0 of the Google Analytics API (Core Reporting API). I registered my application in the Google APIs Console and used OAuth 2.0 for accessing the API.

Since there are no samples yet how to use the Google Analytics API, I used a sample that demonstrates the simplest use case for an OAuth2 service and applied that schema to my context - with success.

(Remark: I also found help to correct the mistakes in the source code of the library Google.Apis.Analytics.v3.dll)

If I use my program, I am required to login to Google, to grant permissions to my application and to copy an authorization code.

I would like to skip this flow by writing my login password into the program code (- in clear text). My problem ist that I can't find this feature in the libraries. In Google Analyitcs Version 2.3, it did work with the simple C# lines:

AnalyticsService asv = new AnalyticsService("");
asv.setUserCredentials(CLIENT_USERNAME, CLIENT_PASS);

Can anybody help me?

michael-ki
  • 73
  • 1
  • 5

2 Answers2

5

Putting in a username and password is a security issue. Google added offline access just for this purpose.

Use a refresh token. You can store that and use it to access your analytics data without having to log in every time which is essentially what you are doing with the username and password set up you described.

Offline access instructions can be found here:

http://code.google.com/apis/accounts/docs/OAuth2WebServer.html#offline

jk.
  • 14,365
  • 4
  • 43
  • 58
  • 4
    It worked. I found a useful [example](http://stackoverflow.com/a/7489566/1151937), which shows how to use Refresh Tokens with Google's Library. – michael-ki Jan 27 '12 at 15:37
  • @michael-ki Excellent! Glad you found an example. +1 for updating so it can help others. – jk. Jan 27 '12 at 15:38
  • Hi Michael. Do you have a working example that you could share? I'm really struggling with this myself, and a working solution would really be a big help. Thanks. – Sam Delaney May 11 '12 at 21:37
  • i am also looking for PHP version to access offline – Harsha M V Jun 13 '12 at 15:47
0

Well this is an old question. But here is a code example of how to get it working.

PM> install-package google.apis -pre
PM> install-package google.apis.analytics.v3 -pre

Download the client secret json file from Apis console and add it to your project with the name client_secret.Json. You might have to change the properties on it to build action content and copy to out put if newer.

// Autentication
UserCredential credential;
using (var stream = new System.IO.FileStream("client_secret.json", System.IO.FileMode.Open,      System.IO.FileAccess.Read))
 {
  credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
  GoogleClientSecrets.Load(stream).Secrets,
  new[] { AnalyticsService.Scope.AnalyticsReadonly },
  "user", CancellationToken.None, new FileDataStore("Analytics.Auth.Store")).Result;
  }

// creates the service

AnalyticsService service = new AnalyticsService(new BaseClientService.Initializer() {
     HttpClientInitializer = credential,
     ApplicationName = "Analytics API sample",
 });
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449