Christian Bekker
Christian Bekker

Reputation: 1867

Google Apis .Net - Getting Analytics data

I'm trying to use the Google.Apis.Analytics.v3 (1.7.0.81-beta), to get some basic analytics data out. I've been searching a lot for examples and help, but most seems to be deprecated.

This is was i got right now:

var serviceAccountEmail = "[email protected]";

var certificate = new X509Certificate2(@"PATH-TO-MY-PRIVATE-KEY", "notasecret", X509KeyStorageFlags.Exportable);

var credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
    Scopes = new[] { AnalyticsService.Scope.Analytics }
}.FromCertificate(certificate));

// Create the service.
var gas = new AnalyticsService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = "Tester",
});

var r = gas.Data.Ga.Get("ga:XXXXX", "2013-01-01", "2014-02-31", "ga:visitors");

//Specify some addition query parameters
r.Dimensions = "ga:pagePath";
r.Sort = "-ga:visitors";
r.MaxResults = 5;

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

This gives me:

Google.Apis.Requests.RequestError
Access Not Configured. Please use Google Developers Console to activate the API for your project. [403]
Errors [
    Message[Access Not Configured. Please use Google Developers Console to activate the API for your project.] Location[ - ] Reason[accessNotConfigured] Domain[usageLimits]
]

In google api console i've already setup a Oauth2 Service Account, and added the email from that, to my analytics account on the page im trying to pull data from. So not sure what ive missed? or if im actually just doing it plain wrong?

Update

After enabling the Analytics Api, i now get:

Google.Apis.Requests.RequestError User does not have sufficient permissions for this profile. [403] 
    Errors [ 
        Message[User does not have sufficient permissions for this profile.] 
        Location[ - ] 
        Reason[insufficientPermissions] 
        Domain[global] 
    ]

Upvotes: 1

Views: 1252

Answers (2)

jk.
jk.

Reputation: 14435

Add the service account email to the Google Analytics account you are trying to access with "Read and Analyze" permissions.

See the steps for fully set up access for a service account in the answers here:

Connecting to Google Analytics API in a Rails app

I know it says "Rails", just look at the steps to set up the service account.

Upvotes: 1

Madhur Ahuja
Madhur Ahuja

Reputation: 22709

Have you enabled Analytics API like shown in image below ?

enter image description here

Upvotes: 0

Related Questions