how to use google analytics with stored refresh token

691 views
Skip to first unread message

dmi...@svitsoft.com

unread,
Nov 21, 2014, 9:30:04 AM11/21/14
to google-analytics...@googlegroups.com
How i can use Google.Apis.Analytics.v3 version 1.9.0.104 with stored refresh token in my database?

Kushan Shah

unread,
Nov 22, 2014, 8:53:41 AM11/22/14
to google-analytics...@googlegroups.com
Depending on the type of your application you would have to use the Refresh Token to obtain a new Access Token. Code Samples and OAuth 2.0 Flow are documented here

Hope that helps,

dmi...@svitsoft.com

unread,
Nov 24, 2014, 3:10:13 AM11/24/14
to google-analytics...@googlegroups.com
I read this doc.
We have experimental code.
var store = new FileDataStore("C:\\!Sources\\Tokens\\Analytics.Api.Auth.Store");
            IAuthorizationCodeFlow flow =
            new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
            {
                ClientSecrets = new ClientSecrets
                {
                    ClientId = "id",
                    ClientSecret = "secred"
                },
                Scopes = new[] { AnalyticsService.Scope.AnalyticsReadonly },
                DataStore = store
            });
            Google.Apis.Auth.OAuth2.Responses.TokenResponse token = new Google.Apis.Auth.OAuth2.Responses.TokenResponse();
            token.RefreshToken = "token";
            Google.Apis.Auth.OAuth2.UserCredential credential = new Google.Apis.Auth.OAuth2.UserCredential(flow, "Random", token);
            var task=credential.RefreshTokenAsync(CancellationToken.None);// Ok. Access token !=null but Scope==null??????
            task.Wait();
            AnalyticsService service = new AnalyticsService(new BaseClientService.Initializer()
            {
                ApiKey = "apikey",
                HttpClientInitializer = credential,                
                ApplicationName = "Analytics API Sample",
            });
            var param = service.Data.Ga.Get("ga:78203720", "2014-11-05", "2014-11-19", "ga:users");

Function service.Data.Ga.Get redirect to auth page. Maybe i use wrong function to obtain access token .

Субота, 22 листопада 2014 р. 15:53:41 UTC+2 користувач Kushan Shah написав:
Reply all
Reply to author
Forward
0 new messages