0
votes

I'm trying access the Analytics information with API v3

        // This is the physical path to the key file you downloaded when you created your Service Account
    String key_file = @"C:\Users\path\XXXXX-privatekey.p12";

    // Is the "Email Address", not the "Client ID" one!!!
    String client_id = "[email protected]";

    // Probably the password for all is "notasecret"
    String key_pass = "notasecret";


       private void button1_Click(object sender, System.EventArgs e)
    {
        try
        {
            String scope_url = "https://www.googleapis.com/auth/" + Google.Apis.Analytics.v3.AnalyticsService.Scopes.Analytics.ToString().ToLower();
            //scope_url = "https://www.googleapis.com/auth/analytics";
            //scope_url = "https://www.googleapis.com/auth/analytics.readonly";

            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_url };
            OAuth2Authenticator<AssertionFlowClient> auth = new OAuth2Authenticator<AssertionFlowClient>(client, AssertionFlowClient.GetState);                
            AnalyticsService gas = new AnalyticsService(auth);

            // Creating our query
            DataResource.GaResource.GetRequest r = gas.Data.Ga.Get("ga:1983192", "2013-11-01", "2013-11-30", "ga:visits,ga:pageviews");
            //r.Dimensions = "ga:date";                

            //d: Execute and fetch the results of our query
            GaData d = r.Fetch();

            // At this point, d should contain the number of visitors you got between dates
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

But I always get the next error

Google.Apis.Requests.RequestError User does not have any Google Analytics account. [403] Errors [ Message[User does not have any Google Analytics account.] Location[ - ] Reason[insufficientPermissions] Domain[global] ]

I create the application in https://cloud.google.com/console and as you can you see create credentials Have active Analytics account and authorized in APIs section on the console.

What can happen?

1

1 Answers

1
votes

Finally!! the answer is here!!! Google Analytics throws 403 error

If you login to Google Analytics using the web interface you'll see the following pattern in URL:

/a12345w654321p9876543/

The number following the p is the AccountID, so

DataResource.GaResource.GetRequest r = gas.Data.Ga.Get("ga:9876543", "2013-11-01", "2013-11-30", "ga:visits,ga:pageviews");