liuqiaosz / google-api-dotnet-client

Automatically exported from code.google.com/p/google-api-dotnet-client
Apache License 2.0
0 stars 0 forks source link

Google Analytics in mvc #474

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago

    public IList<int> GetStats()
        {
            string scope = AnalyticsService.Scopes.AnalyticsReadonly.GetStringValue();
            //UPDATE this to match your developer account address. Note, you also need to add this address 
            //as a user on your Google Analytics profile which you want to extract data from (this may take
            //up to 15 mins to recognise)
            //string client_id = "493694130884-m0jlcf6dabtnnjf5ji3jpfk5uh1m5ose@developer.gserviceaccount.com";

            string client_id = "372469488083-8imjoam8une71sevtmn3urtvulq9kcom.apps.googleusercontent.com";

            //836224293376-3524as8ngv6jia4l9qsf7dd4snr1utds@developer.gserviceaccount.com
            //UPDATE this to match the path to your certificate
            //string key_file = @"E:\8678acc035aa1965cbf36543dcf30f330c7549d2-privatekey.p12";
            string key_file = @"E:\7b6826181f30240ce74035b2d09d6c2869885b6c-privatekey.p12";

            //ddd39a925dad7f817b609931ddd91cbf03aa7e81-privatekey
            //string key_pass = "notasecret";
            string key_pass = "notasecret";

            AuthorizationServerDescription desc = GoogleAuthenticationServer.Description;

            X509Certificate2 key = new X509Certificate2(key_file, key_pass, X509KeyStorageFlags.Exportable);

            AssertionFlowClient client =
                new AssertionFlowClient(desc, key) { ServiceAccountId = client_id, Scope = scope };

            OAuth2Authenticator<AssertionFlowClient> auth =
                new OAuth2Authenticator<AssertionFlowClient>(client, AssertionFlowClient.GetState);

            AnalyticsService gas = new AnalyticsService(new BaseClientService.Initializer() { Authenticator = auth });

            //UPDATE the ga:nnnnnnnn string to match your profile Id from Google Analytics
            DataResource.GaResource.GetRequest r =
                gas.Data.Ga.Get("ga:88028792", "2014-01-01", "2014-07-31", "ga:visitors");

            r.Dimensions = "ga:pagePath";
            r.Sort = "-ga:visitors";
            r.MaxResults = 5;

            GaData d = r.Execute();

            IList<int> stats = new List<int>();

            for (int y = 0; y < d.Rows.Count; y++)
            {
                stats.Add(Convert.ToInt32(d.Rows[y][1]));
            }

            return stats;
        }

This is giving error of the auth.token (server error 404)

Original issue reported on code.google.com by rakhi001...@gmail.com on 3 Jul 2014 at 10:09

GoogleCodeExporter commented 9 years ago
Still relevant?

Original comment by pele...@google.com on 31 Jul 2014 at 2:56