c# - Code trying to find ServiceAccountCredential.cs file while accessing Google Analytics using OAUTH 2.0 -
i trying access google analytics data ga:visits , ga:transactions using oauth 2.0 authentication. have followed following steps :
- enabled google analytics api on https://code.google.com/apis/console/ website.also, created client id , saved .p12 key
- the generated email id provided read , analyze access on analytics website.
- i have created windows form application , downloaded nuget package using package command:- install-package google.apis.analytics.v3 references including google.apis,google.apis.analytics.v3,google.apis.auth,google.apis.auth.platformservices,google.apis.core,google.apis.platformservices added project
further, while accessing data facing error :-
locating source
c:\code\google.com\google-api-dotnet-client\default\tools\google.apis.release\bin\debug\test\default\src\googleapis.auth.dotnet4\oauth2\serviceaccountcredential.cs
while following line of code run :
var credential = new serviceaccountcredential(new serviceaccountcredential.initializer(serviceaccountemail) { scopes = new[] { analyticsservice.scope.analyticsreadonly } }.fromcertificate(cert)); could please me error. please find below code used :-
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; using google.apis.analytics.v3; using system.security.cryptography.x509certificates; using google.apis.auth.oauth2; using google.apis.services; namespace windowsformsapplication5 { public partial class form1 : form { public form1() { initializecomponent(); } private void form1_load(object sender, eventargs e) { string keyfilepath = "c:\\fetch ga data-348e7435108b.p12"; string serviceaccountemail= "xx@developer.gserviceaccount.com"; string keypassword = "notasecret"; string websitecode = "8482xxxx"; analyticsservice service = null; //loading key file var certificate = new x509certificate2(keyfilepath, keypassword, x509keystorageflags.exportable); //add scopes var scopes = new string[] { analyticsservice.scope.analytics,analyticsservice.scope.analyticsedit,analyticsservice.scope.analyticsmanageusers,analyticsservice.scope.analyticsreadonly }; //create new serviceaccountcredential var credential = new serviceaccountcredential(new serviceaccountcredential.initializer(serviceaccountemail) { //scopes = scopes scopes = new[] { analyticsservice.scope.analyticsreadonly } }.fromcertificate(cert)); //create service service = new analyticsservice(new baseclientservice.initializer() { httpclientinitializer = credential }); dataresource.garesource.getrequest request = service.data.ga.get( "ga:" + websitecode, "2015-05-27", "2015-05-27","ga:visits"); request.dimensions = "ga:year,ga:month,ga:day"; var data = request.execute(); } } }
i not sure got example not close how should using service account access google analytics api.
string[] scopes = new string[] { analyticsservice.scope.analytics, // view , manage google analytics data analyticsservice.scope.analyticsmanageusers}; // view google analytics data string keyfilepath = @"c:\file.p12" ; // found in developer console string serviceaccountemail = "xx@developer.gserviceaccount.com"; // found in developer console //loading key file var certificate = new x509certificate2(keyfilepath, "notasecret", x509keystorageflags.exportable); serviceaccountcredential credential = new serviceaccountcredential( new serviceaccountcredential.initializer(serviceaccountemail) { scopes = scopes }.fromcertificate(certificate)); analyticsservice service = new analyticsservice(new baseclientservice.initializer() { httpclientinitializer = credential, applicationname = "analytics api sample", }); code ripped google analytics authentication c# tutorial series
tip service account setup
take service account email address google developer console, need give email address access our google analytics data. adding other user in admin section of google analytics website. add service account email address user @ account level must account level. enable service account access google analytics account in question. did mention must @ account level?
Comments
Post a Comment