The API key is intended only for access to public data, which means that you can use it only with api metadata .
Google Analytics data is personal data owned by the user. Since this is your own data that you are trying to access, you should create a service account. Service accounts are pre-authorized. Go to the Google Developer Console and create the service account credentials . Then enter the email address of the service account and add it as a user at the ACCOUNT level in the admin section of the Google Analytics website. It MUST be an ACCOUNT level. You have now authorized the service account to access the data for this account.
Auth Service Account
// Copyright 2017 DAIMTO : www.daimto.com // // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with // the License. You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on // an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. //------------------------------------------------------------------------------ // <auto-generated> // This code was generated by DAIMTO-Google-apis-Sample-generator 1.0.0 // Template File Name: ServiceAccount.tt // Build date: 01/02/2017 22:31:50 // C# generater version: 1.0.0 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ // About // // Unoffical sample for the AnalyticsReporting v4 API for C#. // This sample is designed to be used with the Google .Net client library. (https://github.com/google/google-api-dotnet-client) // // API Description: Accesses Analytics report data. // API Documentation Link https://developers.google.com/analytics/devguides/reporting/core/v4/ // // Discovery Doc https://www.googleapis.com/discovery/v1/apis/AnalyticsReporting/v4/rest // //------------------------------------------------------------------------------ // Installation // // This sample code uses the Google .Net client library // // NuGet package: // // Location: https://www.nuget.org/packages/Google.Apis.AnalyticsReporting.v4/ // Install Command: PM> Install-Package Google.Apis.AnalyticsReporting.v4 // //------------------------------------------------------------------------------ using Google.Apis.AnalyticsReporting.v4 using Google.Apis.Auth.OAuth2; using Google.Apis.Services; using System; using System.IO; using System.Security.Cryptography.X509Certificates; namespace GoogleSamplecSharpSample.AnalyticsReportingv4.Auth { public class static ServiceAccountExample { /// <summary> /// Authenticating to Google using a Service account /// Documentation: https://developers.google.com/accounts/docs/OAuth2
Code derived from my google-api-dotnet sample project for reporting APIs
Once you have created your service, you can use the code on the C # sample page
I have an old service account tutorial with V3. Analytics Authentication
DaImTo
source share