为什么Google Cloud Postmaster API返回一个空数组?
我有一个服务帐户。
我写了一个获得令牌的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
问题可能是什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用两个不同的身份。只有一个身份将获取域列表。您正在使用服务帐户进行身份验证。这些域已注册为用户的身份。由于服务帐户没有注册域,因此列表为空。
解决方案是使用用户的身份授权此API调用。这意味着使用 google oauth 2.0
邮政局长工具api api atapi state:
用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:
Authorizing requests with OAuth 2.0
管理用户 - &gt; adduser(+) - &gt;输入您的服务帐户电子邮件
Manage Users -> adduser(+) -> input your service account email