首页 > 解决方案 > 尝试将网络媒体资源链接到 AdWords (Google Ads) 帐户时出现 InsufficientPermission (403)

问题描述

我试图为使用 Analytics Management API 时似乎相对常见的问题找到一个解决方案,不幸的是,没有一个答案是有帮助的。

我开始将 Analytics Management API 与对我的 Analytics 帐户具有完全访问权限的服务帐户一起使用。它在帐户级别被授予编辑、协作、读取和分析以及管理用户权限,这意味着它们也在属性级别和视图级别被继承。

我可以成功获得我可以访问的帐户,并插入新的网络资产。

但是,我无法将我的 Google Ads 帐户与任何网络媒体资源相关联。每当我调用 API 时,我都会收到 403 错误:

{"error":{"errors":[{"domain":"global","reason":"insufficientPermissions","message":"User does not have sufficient permissions for this web property."}],"code":403,"message":"User does not have sufficient permissions for this web property."}}

我检查了很多次,对于每个级别(帐户、属性甚至视图)我的服务帐户具有哪些权限,总是有编辑、协作、阅读和分析以及管理用户。

以下是我尝试关联 Google Ads 帐户的方法:

<?php
require_once __DIR__ . '/vendor/autoload.php';
$KEY_FILE_LOCATION = __DIR__ . '/service-account-credentials.json';
$client = new Google_Client();
$client->setApplicationName("Hello Analytics Management API");
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->setScopes([
  'https://www.googleapis.com/auth/plus.login',
  'https://www.googleapis.com/auth/analytics',
  'https://www.googleapis.com/auth/analytics.edit',
  'https://www.googleapis.com/auth/analytics.provision',
  'https://www.googleapis.com/auth/analytics.manage.users',
  'https://www.googleapis.com/auth/analytics.readonly'
]);
$analytics = new Google_Service_Analytics($client);
$adWordsAccount = new Google_Service_Analytics_AdWordsAccount();
$adWordsAccount->setCustomerId("XXX-XXX-XXXX");
$adWordsLink = new Google_Service_Analytics_EntityAdWordsLink();
$adWordsLink->setName("TestLink");
$adWordsLink->setAdWordsAccounts([$adWordsAccount]);
try {
  $analytics->management_webPropertyAdWordsLinks->insert(
    999999999, // Analytics Account ID
    "UA-999999999-1", // Tracking ID
    $adWordsLink
  );
} catch (apiServiceException $e) {
  print 'There was an Analytics API service error '
        . $e->getCode() . ':' . $e->getMessage();

} catch (apiException $e) {
  print 'There was a general API error '
      . $e->getCode() . ':' . $e->getMessage();
}

我知道我当然使用了比我需要的更多的范围,但使用只https://www.googleapis.com/auth/analytics.edit给出了相同的结果。

以下是service-account-credentials.json我使用的(使用我通过 Google API 控制台生成的密钥):

{
  "type": "service_account",
  "project_id": "...-230314",
  "private_key_id": "<PRIVATE_KEY_ID>",
  "private_key": "<PRIVATE_KEY>",
  "client_email": "dev-...@ga-....iam.gserviceaccount.com",
  "client_id": "<CLIENT_ID>",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/dev-...%40...-230314.iam.gserviceaccount.com"
}

预先感谢您对我们的支持!

多里安

标签: google-analyticsgoogle-analytics-apigoogle-ads-api

解决方案


为了能够链接 Google Analytics 和 Google Ads,执行链接的帐户必须是 Google Analytics 网络媒体资源中的编辑(显然您已经这样做了),但还需要是 Google Ads 帐户中的管理员。

不是您的个人/公司 Google 帐户,而是在这种情况下是服务帐户本身。client_email您在凭据中包含在您的财产中的电子邮件。

参考:谷歌帮助中心

警告:

我不是 100% 肯定你甚至能够做到这一点。Google Ads 可能需要用户确认才能添加到帐户中,而您无权访问服务帐户的邮箱这一事实可能会阻止您执行该确认。同样我不确定这一点,所以我会尝试,但如果您无法将该用户添加到 Google Ads 帐户,您将不得不找到另一种方法来使用您自己的电子邮件而不是服务帐户。


推荐阅读