我试图为使用 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"
}
预先感谢您对我们的支持!
多里安