无法访问 Adsense 管理 API

Can not access the Adsense Management API

无论我怎样尝试都无法访问 AdSense API。我已经尝试了 service accountOAuth 身份验证。

我想要完成的事情:

创建一个机器人来获取 Adsense 赚取的收入并将其存储在内部数据库中以跟踪一段时间内的收入。我不想出现身份验证问题,我想要 服务器到服务器 配置。

我所知道的是您可以在 OAuth 下拥有访问令牌和刷新令牌。我还不能使它们在 Adsense 的任何可用 API 下工作。注意:我已经构建了一个 API 来使用 AdWords 没问题。

错误:(认证类型之间,我无法通过这些)

到目前为止我做了什么:

要使用的库(尝试):

https://developers.google.com/api-client-library/php/

https://github.com/google/google-api-php-client

https://github.com/google/google-api-php-client-services

(已过时)https://github.com/googleads/googleads-adsense-examples

示例代码:(使用服务帐号)

$client = new \Google_Client();
$client->setAuthConfig(WRITEPATH . 'auth/adsense-client.json');
$client->setIncludeGrantedScopes(true);
$client->addScope('https://www.googleapis.com/auth/adsense');
$client->setSubject('email@example.com');

// trying to fire the services
$service = new \Google_Service_AdSense($client);
$report  = $service->accounts_reports->generate('clientId', $startDate, $endDate, $optParams);

此脚本触发 "Client is unauthorized to retrieve access tokens using this method." 错误消息。但是,正在使用的电子邮件地址能够访问 adsense 帐户,APIs 已在管理域级别启用和配置。

有没有更简单的方法?有谁能指引我正确的方向吗?

服务帐号

我不太确定服务帐户是否适用于 adsence。您可以尝试访问 adsence 网站并在 manageUsers 下添加服务帐户电子邮件地址。这里的问题是它会一直待定,我认为是因为服务帐户需要确认它已被添加。没有办法做到这一点。

管理域级别

如果你想为此添加域范围的授权,我也不会为此烦恼,据我所知,它也不被支持。

Client is unauthorized to retrieve access tokens using this method.

至于那个错误,我也许可以提供帮助。这意味着您已经获取了为服务帐户下载的凭据文件,并尝试将其与 Oauth2 的代码一起使用。反之亦然。再次下载凭据文件并重试。

我有一些示例代码可能会有所帮助 here