【发布时间】:2020-10-14 20:14:19
【问题描述】:
我正在尝试阅读我过去的交易。我已经使用 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
【问题讨论】:
标签: node.js rest curl paypal paypal-rest-sdk