Reputation: 1
I am trying to access Google Analytics data for ga:visits
and ga:transactions
using OAUTH 2.0 authentication. I have followed the following steps :
Further, while accessing the data am facing an error :-
Locating source for
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 the following line of code is run :
var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { AnalyticsService.Scope.AnalyticsReadonly }
}.FromCertificate(cert));
Could you please help me with this error. Please find below the 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= "[email protected]";
string keyPassword = "notasecret";
string websiteCode = "8482xxxx";
AnalyticsService service = null;
//loading the 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 a new ServiceAccountCredential
var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
//Scopes = scopes
Scopes = new[] { AnalyticsService.Scope.AnalyticsReadonly }
}.FromCertificate(cert));
//Create a 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();
}
}
}
Upvotes: 0
Views: 2614
Reputation: 117146
I am not sure where you got that example from but its not even close to how you should be using a service account to access Google Analytics api.
string[] scopes =
new string[] {
AnalyticsService.Scope.Analytics, // view and manage your Google Analytics data
AnalyticsService.Scope.AnalyticsManageUsers}; // View Google Analytics data
string keyFilePath = @"c:\file.p12" ; // found in developer console
string serviceAccountEmail = "[email protected]"; // found in developer console
//loading the 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 from Google Analytics Authentication C# tutorial series
TIP Service Account Setup
Take the service account email address from the Google Developer console, we need to give this email address access to our Google Analytics Data. We do that by adding it as we would any other user in the Admin Section of the Google Analytics Website. Add the service account email address as a user at the account level it must be the account level. This will enable your service account access to the Google Analytics account in question. Did I mention it must be at the ACCOUNT Level?
Upvotes: 1