【问题标题】:Error refreshing the OAuth2 token { "error" : "invalid_grant" }刷新 OAuth2 令牌时出错 { "error" : "invalid_grant" }
【发布时间】:2014-06-28 16:50:44
【问题描述】:

请帮助我理解错误在哪里。

API

Google Api PHP v3,key.p12 是 .zip 集合 API 中的文件(或者我在哪里找到它?)

我用 xxxxxxxxxx 掩盖了我的数据

对不起我的英语

gaphp.php

<?php

      require_once 'Google/Client.php';
      require_once 'Google/Service/Analytics.php';

      $client_id = 'xxx-xxx.apps.googleusercontent.com';
      $service_account_name = 'xxx-xxx@developer.gserviceaccount.com';
      $keyfile = 'key.p12';
      $redirect_url = 'http://example.com/service/ga/gaphp.php';
      $client_secret = 'xxxxxxxxxxxxxxxxxxxxx';

      // Initialise the Google Client object
      $client = new Google_Client();
      $client->setApplicationName('My application');
      $client->setRedirectUri($redirect_url);
      $client->setClientSecret($client_secret);

      $client->setAssertionCredentials(
              new Google_Auth_AssertionCredentials(
                  $service_account_name,
                  array('https://www.googleapis.com/auth/analytics'),
                  file_get_contents($keyfile)
              )
      );

      // Get this from the Google Console, API Access page
      $client->setClientId($client_id);
      $client->setAccessType('offline_access');
      $analytics = new Google_Service_Analytics($client);

      // We have finished setting up the connection,
      // now get some data and output the number of visits this week.

      // Your analytics profile id. (Admin -> Profile Settings -> Profile ID)
      $analytics_id   = 'ga:xxxxxx';
      $lastWeek       = date('Y-m-d', strtotime('-1 week'));
      $today          = date('Y-m-d');

      try {
          $results = $analytics->data_ga->get($analytics_id, $lastWeek, $today,'ga:visits');
          echo '<b>Number of visits this week:</b> ';
          echo $results['totalsForAllResults']['ga:visits'];
      } catch(Exception $e) {
          echo 'There was an error : - ' . $e->getMessage();
      }

?>

错误

There was an error : - Error refreshing the OAuth2 token, message: 'Protection: 1; mode=block Server: GSE Alternate-Protocol: 443:quic Transfer-Encoding: chunked { "error" : "invalid_grant" }'

【问题讨论】:

标签: google-api-php-client


【解决方案1】:

我昨天刚刚使用服务帐户进行了分析 - 所以我也许可以提供帮助。从控制台https://console.developers.google.com/project,您可以选择您的项目并转到“APIS & AUTH”。确保打开了分析 API。并确保您拥有服务帐户“凭据”。确保为您的 Google Analytics(分析)资源授​​予服务帐户电子邮件地址权限。您还需要为您的服务帐户获取 P12 密钥文件,并在您的服务器上提供该密钥文件。我实际上为我的 $key_file_location 变量使用了完整的文件路径。

我在 GitHub 上使用了这段代码:https://github.com/google/google-api-php-client/blob/master/examples/service-account.php

该示例适用于 Books API - 因此只需将“Books”替换为“Analytics”,将“books”替换为“analytics”。填写您的服务帐户凭据,并记得将您的服务帐户电子邮件添加到您的 google 分析属性。

【讨论】:

    【解决方案2】:

    您只需确认您的服务帐户电子邮件 ID 与 google analytics 帐户相关联,并具有适当的权限。

    第二件事是确保.P12文件在上述步骤编译后生成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-11
      • 2014-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多