Google Analytics API v3 で 「Error:"unauthorized_client", Description:"Unauthorized client or scope in request.", Uri:""」 エラーが発生する
このページのタグ:[C#] [Google Analytics API]
Google Analytics API v3 で 「Error:"unauthorized_client", Description:"Unauthorized client or scope in request.", Uri:""」 エラーが発生する場合の対処法を紹介します。

原因

原因はいくつか考えられますが、User = userAccountEmail を指定している場合に上記のエラーが発生するケースがあります。

エラーの発生するコード


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 System.Security.Cryptography.X509Certificates;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Analytics.v3;
using Google.Apis.Analytics.v3.Data;
using Google.Apis.Services;

namespace GoogleAnalyticsPVGet
{
  public partial class FormMain : Form
  {
    public FormMain()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      X509Certificate2 certificate = new X509Certificate2(@"apikey.p12", "notasecret", X509KeyStorageFlags.Exportable);

      string serviceAccountEmail = "012345678901-abcdefghijklmnopqrstuvwxyz012345@developer.gserviceaccount.com";
      string userAccountEmail = "ipentec.test.my.email@gmail.com";
      ServiceAccountCredential credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
      {
        User = userAccountEmail,
        Scopes = new[] { AnalyticsService.Scope.Analytics, AnalyticsService.Scope.AnalyticsReadonly }
      }
      .FromCertificate(certificate));

      AnalyticsService service = new AnalyticsService(new BaseClientService.Initializer
      {
        HttpClientInitializer = credential,
        ApplicationName = "sample app",
      });

      string date = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");

      DataResource.GaResource.GetRequest request = service.Data.Ga.Get("ga:00000000", date, date, "ga:pageviews");

      GaData data = request.Execute();

      textBox1.Text += data.Rows[0][0];
    }
  }
}

対策

User = userAccountEmail の設定を外すと改善される場合があります。

上記のコードを以下に変更します。

変更前


      string serviceAccountEmail = "012345678901-abcdefghijklmnopqrstuvwxyz012345@developer.gserviceaccount.com";
      string userAccountEmail = "ipentec.test.my.email@gmail.com";
      ServiceAccountCredential credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
      {
        User = userAccountEmail,
        Scopes = new[] { AnalyticsService.Scope.Analytics, AnalyticsService.Scope.AnalyticsReadonly }
      }
      .FromCertificate(certificate));

変更後


      string serviceAccountEmail = "012345678901-abcdefghijklmnopqrstuvwxyz012345@developer.gserviceaccount.com";
      ServiceAccountCredential credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
      {
        Scopes = new[] { AnalyticsService.Scope.Analytics, AnalyticsService.Scope.AnalyticsReadonly }
      }
      .FromCertificate(certificate));
プライバシー    iPentecについて
iPentec all rights reserverd. (ISDC)