我在使用 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/