微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用Google My Business API获取帐户详细信息列表?

如何解决如何使用Google My Business API获取帐户详细信息列表?

我正在尝试列出所有有权管理其帐户的帐户。但是,当我使用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",};
          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>

解决方法

https://github.com/google/google-my-business-samples/blob/master/java/AccountsList.java 在这里您可以找到答案,结果是一个JSONObject并可以对其进行解析。 记得在项目文件夹中包含client_secrets.json文件

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。