NodeJS 和 googleapis,POST 请求不工作?

NodeJS & googleapis, POST request not working?

我在使用 hapi 的 NodeJS 中遇到 google api 问题。我使用 npm install googleapis 和 google-auth-library 下载了文件。一切设置正确。

function listData(auth) {
  let webmasters = google.webmasters('v3');
  webmasters.searchanalytics.query({
    auth: auth,
    siteUrl: 'http%3A%2F%2Falanmroczek.pl%2F',
    startDate: "2016-09-20",
    endDate: "2016-10-14"
  }, function(err, response) {
    if (err) {
      console.log('The API returned an error: ' + err);
      return;
    }

    console.log(response);
  });
}

我被这个应用程序授权到那个范围(如果不是它会抛出一个错误,所以我确信这部分没问题)。当我列出我的 gmail 文件夹后,它工作得很好。错误的可能只是这部分代码或者googleapis。对我来说奇怪的是:

当我console.log请求Google时API:

 protocol: 'https:',
 slashes: true,
 auth: null,
 host: 'www.googleapis.com',
 port: null,
 hostname: 'www.googleapis.com',
 hash: null,
 search: '?startDate=2016-09-20&endDate=2016-10-14',
 query: 'startDate=2016-09-20&endDate=2016-10-14',
 pathname: '/webmasters/v3/sites/http%3A%2F%2Falanmroczek.pl%2F/searchAnalytics/query',
 path: '/webmasters/v3/sites/http%3A%2F%2Falanmroczek.pl%2F/searchAnalytics/query?startDate=2016-09-20&endDate=2016-10-14',
 href: 'https://www.googleapis.com/webmasters/v3/sites/http%3A%2F%2Falanmroczek.pl%2F/searchAnalytics/query?startDate=2016-09-20&endDate=2016-10-14' },

查询、路径和 href 看起来像普通的 GET,我不知道为什么。我试图覆盖它,但我仍然得到 "backend error".

编辑:Gmail 通过 GET 列出文件夹,这就是我指出 POST 的原因。

从 Google API 调用返回的后端错误将有 503 作为其错误代码,这意味着 "Service Unavailable"。

在此处检查各种 API 及其状态,并确保您尝试访问的服务已启动并且 运行 正常。

https://www.google.com/appsstatus#hl=en&v=status

希望对您有所帮助!

我在处理类似的代码。这个参考对我有帮助。

随便看看:NodeJS & Goodgle Express API

node.js 的 googleapis 可能有问题。它通过 GET 发送数据不需要 POST 和 JSON。我们必须手动完成。