javascript - NodeJS 和 googleapis,POST 请求不起作用?

标签 javascript node.js hapi.js google-search-api

我在使用 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。对我来说奇怪的是:

当我向 Google API 发出 console.log 请求时:

 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,我不知道为什么。我尝试覆盖它,但仍然收到“后端错误”。

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

最佳答案

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

关于javascript - NodeJS 和 googleapis,POST 请求不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40044668/

相关文章:

javascript - 检测 AUI 对话框中按下的 Enter 键

VirtualBox CentOS 机器上的node.js,没有连接?

node.js - npm 安装错误 "Please try running this command again as root/Administrator."

javascript - 使用d3js通用函数画线

javascript - 带有雷达循环覆盖的 Google map API v3 绘图,并自动更新雷达

javascript - React-chartjs-2 圆环图导出为png

node.js - 如何在 h2o2 代理中向请求 header 添加值?

node.js - 如何使用 Sequelize 在 Express 中创建常见的 CRUD 操作?

javascript - Hapi v17 - 文件响应的 Joi 架构

javascript - 引用错误: For request with hapi/Node. js