node.js - 为什么我在尝试获取我过去的贝宝交易时会收到此错误?
问题描述
我正在尝试阅读我过去的交易。我已经使用 curl 工具完成了这项工作,但我无法在 nodeJS 中使用它。我正在尝试使用请求 api。从贝宝获取令牌就可以了。只是想不通这样做
curl -v -X GET https://api.paypal.com/v1/reporting/transactions?start_date=2020-05-20T01:01:01-00:00&end_date=2020-06-19T01:01:01-00:00 \
-H "Content-Type: application/json" \
-H "Authorization: Bearer access-token"
在 NodeJS 目前我有这个。有人可以帮我弄清楚吗?
var request = require('request');
let token;
request.post({
uri: "https://api.paypal.com/v1/oauth2/token",
headers: {
"Accept": "application/json",
"Accept-Language": "en_US",
"content-type": "application/x-www-form-urlencoded"
},
auth: {
'user': client_id,
'pass': secret,
},
form: {
"grant_type": "client_credentials"
}
}, function (error, response, body) {
let resp = JSON.parse(body);
token = resp.access_token;
console.log(token)
if (error)
console.log(error)
getTransactions();
});
function getTransactions() {
if (!token) {
console.error("Could not get token");
return;
}
request.post({
uri: "https://api.paypal.com/v1/reporting/transactions?start_date=2020-05-20T01:01:01-00:00&end_date=2020-06-19T01:01:01-00:00",
headers: {
"content-type": "application/json"
},
auth: {
bearer: token,
}
}, function (error, response, body) {
console.log(body);
console.log(error)
console.log(JSON.stringify(response))
});
}
错误:
"statusCode":404,"body":"","headers":{"content-length":"0",
"connection":"close","date":"Sun, 21 Jun 2020 00:35:15 GMT",
"cache-control":"max-age=0, no-cache, no-store, must-revalidate",
"paypal-debug-id":"207f5d4c9341",
"strict-transport-security":"max-age=31536000; includeSubDomains"},"request":{"uri":{"protocol":"https:","slashes":true,"auth":null,
"host":"api.paypal.com","port":443,"hostname":"api.paypal.com",
"hash":null,
"search":"?start_date=2020-05-20T01:01:01-00:00&end_date=2020-06-19T01:01:01-00:00",
"query":"start_date=2020-05-20T01:01:01-00:00&end_date=2020-06-19T01:01:01-00:00",
"pathname":"/v1/reporting/transactions",
"path":"/v1/reporting/transactions?start_date=2020-05-20T01:01:01-00:00&end_date=2020-06-19T01:01:01-00:00",
"href":"https://api.paypal.com/v1/reporting/transactions?start_date=2020-05-20T01:01:01-00:00&end_date=2020-06-19T01:01:01-00:00"},
"method":"POST","headers":{"Accept":"application/json",
"Accept-Language":"en_US","content-type":"application/x-www-form-urlencoded",
"authorization":"Bearer accesstoken_is_my_little_secret",
"content-length":0}}}
index.js:38
解决方案
您的 NodeJS 正在发送 POST 请求而不是 GET,因此是 404
推荐阅读
- matlab - 如何在matlab中创建图像数据集
- react-native - 如何知道是什么导致我的 React Native 组件挂载?
- git - 错误:不能用单个值覆盖多个值
- c++ - 如何为 Windows UI 开发生成 Resource.h 文件
- ruby-on-rails - 在 AWS Elastic Beanstalk 上部署应用程序时出现 Ruby 版本错误
- android - 关闭应用程序共享首选项后布尔值为 false
- postgresql - 我们可以在 PostgreSQL 中使用“name”作为列名吗?
- tableau-api - 等于最大日期的日期在 Tableau 中不起作用
- algorithm - 小于 O(N2) 的数组中所有元素的总和
- rest - 如何使用 REST API 访问与套件 crm 中的模块相关的表的数据