【问题标题】:bad request while using google index api in javascript在 javascript 中使用 google index api 时出现错误请求
【发布时间】:2020-09-15 20:07:29
【问题描述】:

你好 evrey,我想制作一个脚本,通过使用来自 http://localhost:8000/test.html 的 google indexing api 来更新我网站的新网址 我将 http://localhost:8000 添加到客户端 ID 中的 Authorized JavaScript origins 但我有错误 400:

Sign-in successful

cb=gapi.loaded_0:228 GET https://content.googleapis.com/discovery/v1/apis/indexing/v3/rest?pp=0&fields=kind%2Cname%2Cversion%2CrootUrl%2CservicePath%2Cresources%2Cparameters%2Cmethods%2CbatchPath%2Cid&key='my api' 400
wh @ cb=gapi.loaded_0:228
g @ cb=gapi.loaded_0:228
xh @ cb=gapi.loaded_0:229
(anonymous) @ cb=gapi.loaded_0:229
d @ cb=gapi.loaded_0:186
b @ cb=gapi.loaded_0:181


Error loading GAPI client for API 
{error: {…}}
error:
code: 400
message: "Request contains an invalid argument."
status: "INVALID_ARGUMENT"
__proto__: Object
__proto__: Object

我将浏览器从 chrome 更改为 edge 一次它可以工作,然后我关闭了我的电脑,但问题再次出现后的第二天。 我正在使用 api expoler 脚本:link 我把脚本和控制台日志作为图像

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>

</head>

<body>
  <script src="https://apis.google.com/js/api.js"></script>
  <script>
    /**
     * Sample JavaScript code for indexing.urlNotifications.publish
     * See instructions for running APIs Explorer code samples locally:
     * https://developers.google.com/explorer-help/guides/code_samples#javascript
     */

    function authenticate() {
      return gapi.auth2.getAuthInstance()
        .signIn({
          scope: "https://www.googleapis.com/auth/indexing"
        })
        .then(function() {
            console.log("Sign-in successful");
          },
          function(err) {
            console.error("Error signing in", err);
          });
    }

    function loadClient() {
      gapi.client.setApiKey("My-API");
      return gapi.client.load("https://content.googleapis.com/discovery/v1/apis/indexing/v3/rest")
        .then(function() {
            console.log("GAPI client loaded for API");
          },
          function(err) {
            console.error("Error loading GAPI client for API", err);
          });
    }
    // Make sure the client is loaded and sign-in is complete before calling this method.
    function execute() {
      return gapi.client.indexing.urlNotifications.publish({
          "resource": {
            "url": "example",
            "type": "URL_UPDATED"
          }
        })
        .then(function(response) {
            // Handle the results here (response.result has the parsed body).
            console.log("Response", response);
          },
          function(err) {
            console.error("Execute error", err);
          });
    }
    gapi.load("client:auth2", function() {
      gapi.auth2.init({
        client_id: "my OAuth client ID"
      });
    });
  </script>
  <button onclick="authenticate().then(loadClient)">authorize and load</button>
  <button onclick="execute()">execute</button>
</body>

</html>

here the console log

【问题讨论】:

    标签: javascript api google-api client bad-request


    【解决方案1】:

    感谢上帝,我尝试 loadClient 然后进行身份验证,它可以工作

      like this <button onclick="loadClient().then(authenticate)">authorize and load</button>
    

    然后我使用 chrome incognito

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
     
    </head>
    
    <body>
      <script src="https://apis.google.com/js/api.js"></script>
      <script>
        /**
         * Sample JavaScript code for indexing.urlNotifications.publish
         * See instructions for running APIs Explorer code samples locally:
         * https://developers.google.com/explorer-help/guides/code_samples#javascript
         */
    
        function authenticate() {
          return gapi.auth2.getAuthInstance()
            .signIn({scope: "https://www.googleapis.com/auth/indexing"})
            .then(function () { console.log("Sign-in successful"); },
              function (err) { console.error("Error signing in", err); });
        }
        function loadClient() {
          gapi.client.setApiKey("My API");
          return gapi.client.load("https://content.googleapis.com/discovery/v1/apis/indexing/v3/rest")
            .then(function () { console.log("GAPI client loaded for API"); },
              function (err) { console.error("Error loading GAPI client for API", err); });
        }
        // Make sure the client is loaded and sign-in is complete before calling this method.
        function execute() {
          return gapi.client.indexing.urlNotifications.publish({
            "resource": {
              "url": "my web site",
              "type": "URL_UPDATED"
            }
          })
            .then(function (response) {
              // Handle the results here (response.result has the parsed body).
              console.log("Response", response);
            },
              function (err) { console.error("Execute error", err); });
        }
        gapi.load("client:auth2", function () {
          gapi.auth2.init({client_id:"My client ID"});
        });
      </script>
      <button onclick="loadClient().then(authenticate)">authorize and load</button>
      <button onclick="execute()">execute</button>
    </body>
    
    </html>
    GAPI client loaded for API
    Sign-in successful
    Response {result: {…}, body: "{↵  "urlNotificationMetadata": {↵    "url": "https…e": "2020-09-15T20:34:07.091671446Z"↵    }↵  }↵}↵", headers: {…}, status: 200, statusText: null}
    

    【讨论】:

      【解决方案2】:
      <script src="https://apis.google.com/js/api.js"></script>
      <script>
        /**
         * Sample JavaScript code for indexing.urlNotifications.publish
         * See instructions for running APIs Explorer code samples locally:
         * https://developers.google.com/explorer-help/code-samples#javascript
         */
      
        function authenticate() {
          return gapi.auth2.getAuthInstance()
              .signIn({scope: "https://www.googleapis.com/auth/indexing"})
              .then(function() { console.log("Sign-in successful"); },
                    function(err) { console.error("Error signing in", err); });
        }
        function loadClient() {
          gapi.client.setApiKey("xxxxxxxxxxxxxxxxxxxx");
          return gapi.client.load("https://content.googleapis.com/discovery/v1/apis/indexing/v3/rest")
              .then(function() { console.log("GAPI client loaded for API"); },
                    function(err) { console.error("Error loading GAPI client for API", err); });
        }
        // Make sure the client is loaded and sign-in is complete before calling this method.
        function execute() {
          return gapi.client.indexing.urlNotifications.publish({
            "resource": {}
          })
              .then(function(response) {
                      // Handle the results here (response.result has the parsed body).
                      console.log("Response", response);
                    },
                    function(err) { console.error("Execute error", err); });
        }
        gapi.load("client:auth2", function() {
          gapi.auth2.init({client_id: "samplexxxxxxxxxxxxxxxxxx.apps.googleusercontent.com"});
        });
      </script>
      <button onclick="authenticate().then(loadClient)">authorize and load</button>
      <button onclick="execute()">execute</button>
      

      不工作和

      <script src="https://apis.google.com/js/api.js"></script>
          <script>
            /**
             * Sample JavaScript code for indexing.urlNotifications.publish
             * See instructions for running APIs Explorer code samples locally:
             * https://developers.google.com/explorer-help/code-samples#javascript
             */
          
            function authenticate() {
              return gapi.auth2.getAuthInstance()
                  .signIn({scope: "https://www.googleapis.com/auth/indexing"})
                  .then(function() { console.log("Sign-in successful"); },
                        function(err) { console.error("Error signing in", err); });
            }
            function loadClient() {
              gapi.client.setApiKey("xxxxxxxxxxxxxxxxxxxx");
              return gapi.client.load("https://content.googleapis.com/discovery/v1/apis/indexing/v3/rest")
                  .then(function() { console.log("GAPI client loaded for API"); },
                        function(err) { console.error("Error loading GAPI client for API", err); });
            }
            // Make sure the client is loaded and sign-in is complete before calling this method.
            function execute() {
              return gapi.client.indexing.urlNotifications.publish({
                "resource": {}
              })
                  .then(function(response) {
                          // Handle the results here (response.result has the parsed body).
                          console.log("Response", response);
                        },
                        function(err) { console.error("Execute error", err); });
            }
            gapi.load("client:auth2", function() {
              gapi.auth2.init({client_id: "samplexxxxxxxxxxxxxxxxxx.apps.googleusercontent.com"});
            });
          </script>
          <button onclick="loadClient().then(authenticate)">authorize and load</button>
          <button onclick="execute()">execute</button>
      

      不工作所有错误; 代码代码:400 消息:“请求包含无效参数。” 状态:“INVALID_ARGUMENT”

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 2022-11-11
      • 2017-04-14
      • 1970-01-01
      • 2013-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      • 2013-08-19
      相关资源
      最近更新 更多