我正在尝试列出所有有权管理其帐户的帐户。但是,当我使用https://mybusiness.googleapis.com/v4/accounts时,它显示(错误1),当我使用访问令牌时,它给出了用户帐户的详细信息,但我想列出所有帐户。但我不知道该怎么办。 当我在/ accounts上发送请求时,将我的客户端ID作为不记名令牌,而当我尝试不使用auth发送时,它将给出此错误
请求缺少必需的身份验证凭据。预期的OAuth 2访问令牌,登录cookie或其他有效的身份验证凭据。参见https://developers.google.com/identity/sign-in/web/devconsole-project。
<html lang="en">
<head>
<script src="https://apis.google.com/js/platform.js" async defer></script>
<script src="https://accounts.google.com/gsi/client" async defer></script>
<script src="https://apis.google.com/js/client.js?onload=authorize"></script>
</head>
<body>
<h2>prac</h2>
<div
id="g_id_onload"
data-client_id="xxx.apps.googleusercontent.com"
data-callback="handleCredentialResponse"
></div>
<script>
function handleCredentialResponse(response) {
const config = {
scope: "https://www.googleapis.com/auth/plus.business.manage",
client_id: "xxx.apps.googleusercontent.com",
};
var requestOptions = {
method: "GET",
redirect: "follow",
};
fetch("https://mybusiness.googleapis.com/v4/accounts", requestOptions)
.then((response) => response.text())
.then((result) => console.log(result))
.catch((error) => console.log("error", error));
gapi.auth2.authorize(config, function (response) {
var requestOptions = {
method: "GET",
redirect: "follow",
};
console.log(response.access_token);
fetch(
`https://mybusiness.googleapis.com/v4/accounts?access_token=${response.access_token}`,
requestOptions
)
.then((response) => response.text())
.then((result) => console.log(result))
.catch((error) => console.log("error", error));
});
}
</script>
</body>
</html>
答案 0 :(得分:0)
https://github.com/google/google-my-business-samples/blob/master/java/AccountsList.java 在这里您可以找到答案,结果是一个JSONObject并可以对其进行解析。 记得在项目文件夹中包含client_secrets.json文件