为什么Google Cloud Postmaster API返回一个空数组?

发布于 2025-02-11 19:21:55 字数 2344 浏览 1 评论 0 原文

我有一个服务帐户。

我写了一个获得令牌的PHP脚本:

<?php

require_once 'JWT_1.php';
require_once 'JWT_KEY_1.php';

use Firebase\JWT\JWT;
use Firebase\JWT\Key;

$privateKey = <<<EOD
-----BEGIN PRIVATE KEY-----
someKey
-----END PRIVATE KEY-----
EOD;

$publicKey = <<<EOD
-----BEGIN PUBLIC KEY-----
someKey
-----END PUBLIC KEY-----
EOD;

$payload = array(
  "iss" => "[email protected]",
  "scope" => "https://www.googleapis.com/auth/postmaster.readonly",
  "aud" => "https://oauth2.googleapis.com/token",
  "exp" => (time() + 600),
  "iat" => time()
);

$jwt = JWT::encode($payload, $privateKey, 'RS256');

$urlToken = 'https://oauth2.googleapis.com/token';
$url = 'https://gmailpostmastertools.googleapis.com/v1/domains';

$payloadToken = array(
  "grant_type" => "urn:ietf:params:oauth:grant-type:jwt-bearer",
  "assertion" => $jwt
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urlToken);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payloadToken);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$tokenAnswer = json_decode(curl_exec($ch), true);
$token = $tokenAnswer['access_token'];
curl_close($ch);

$ch = curl_init();
$headers = array(
  'Authorization: Bearer ' . $token,
  'Accept: application/json'
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING , "");
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
echo curl_exec($ch);

尝试获取域列表时,, 响应是一个空数组。

同时,如果我对该方法进行测试检查,我会得到一个列表: https:// i.sstatic.net/kkvcs.png

问题可能是什么?

I have a service account.

I wrote a PHP script that receives a token:

<?php

require_once 'JWT_1.php';
require_once 'JWT_KEY_1.php';

use Firebase\JWT\JWT;
use Firebase\JWT\Key;

$privateKey = <<<EOD
-----BEGIN PRIVATE KEY-----
someKey
-----END PRIVATE KEY-----
EOD;

$publicKey = <<<EOD
-----BEGIN PUBLIC KEY-----
someKey
-----END PUBLIC KEY-----
EOD;

$payload = array(
  "iss" => "[email protected]",
  "scope" => "https://www.googleapis.com/auth/postmaster.readonly",
  "aud" => "https://oauth2.googleapis.com/token",
  "exp" => (time() + 600),
  "iat" => time()
);

$jwt = JWT::encode($payload, $privateKey, 'RS256');

$urlToken = 'https://oauth2.googleapis.com/token';
$url = 'https://gmailpostmastertools.googleapis.com/v1/domains';

$payloadToken = array(
  "grant_type" => "urn:ietf:params:oauth:grant-type:jwt-bearer",
  "assertion" => $jwt
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urlToken);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payloadToken);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$tokenAnswer = json_decode(curl_exec($ch), true);
$token = $tokenAnswer['access_token'];
curl_close($ch);

$ch = curl_init();
$headers = array(
  'Authorization: Bearer ' . $token,
  'Accept: application/json'
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING , "");
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
echo curl_exec($ch);

When trying to get a list of domains, https://gmailpostmastertools.googleapis.com/v1/domains,
the response is an empty array.

At the same time, if I do a test check of the method, I get a list: https://i.sstatic.net/kkvcS.png

What may the problem be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

糖粟与秋泊 2025-02-18 19:21:55

您正在使用两个不同的身份。只有一个身份将获取域列表。您正在使用服务帐户进行身份验证。这些域已注册为用户的身份。由于服务帐户没有注册域,因此列表为空。

解决方案是使用用户的身份授权此API调用。这意味着使用 google oauth 2.0

邮政局长工具api api atapi state:

所有向邮政局邮政局工具API的请求必须由
身份验证的用户。

用OAuth 2.0授权请求

You are using two different identities. Only one of the identities will fetch the list of domains. You are authenticating with a service account. The domains are registered to a user's identity. Since the service account does not have a registered domain, the list is empty.

The solution is to use a user's identity to authorize this API call. That means using Google OAuth 2.0

The Postmaster Tools API states:

All requests to the Postmaster Tools API must be authorized by an
authenticated user.

Authorizing requests with OAuth 2.0

不交电费瞎发啥光 2025-02-18 19:21:55

管理用户 - &gt; adduser(+) - &gt;输入您的服务帐户电子邮件

Menu

Manage Users -> adduser(+) -> input your service account email

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文