Node.js 如何使用节点检索PayPal REST Api访问令牌
如何使用node获取利用REST Api所需的PayPal访问令牌?一旦您拥有PayPal客户端Id和客户端密码,您可以使用以下各项:Node.js 如何使用节点检索PayPal REST Api访问令牌,node.js,paypal,npm-request,Node.js,Paypal,Npm Request,如何使用node获取利用REST Api所需的PayPal访问令牌?一旦您拥有PayPal客户端Id和客户端密码,您可以使用以下各项: var request = require('request'); request.post({ uri: "https://api.sandbox.paypal.com/v1/oauth2/token", headers: { "Accept": "application/json", "Accept-Lang
var request = require('request');
request.post({
uri: "https://api.sandbox.paypal.com/v1/oauth2/token",
headers: {
"Accept": "application/json",
"Accept-Language": "en_US",
"content-type": "application/x-www-form-urlencoded"
},
auth: {
'user': '---your cliend ID---',
'pass': '---your client secret---',
// 'sendImmediately': false
},
form: {
"grant_type": "client_credentials"
}
}, function(error, response, body) {
console.log(body);
});
{
"scope":"https://api.paypal.com/v1/payments/.* ---and more URL callable with the access-token---",
"access_token":"---your access-token---",
"token_type":"Bearer",
"app_id":"APP-1234567890",
"expires_in":28800
}
如果成功,响应将如下所示:
var request = require('request');
request.post({
uri: "https://api.sandbox.paypal.com/v1/oauth2/token",
headers: {
"Accept": "application/json",
"Accept-Language": "en_US",
"content-type": "application/x-www-form-urlencoded"
},
auth: {
'user': '---your cliend ID---',
'pass': '---your client secret---',
// 'sendImmediately': false
},
form: {
"grant_type": "client_credentials"
}
}, function(error, response, body) {
console.log(body);
});
{
"scope":"https://api.paypal.com/v1/payments/.* ---and more URL callable with the access-token---",
"access_token":"---your access-token---",
"token_type":"Bearer",
"app_id":"APP-1234567890",
"expires_in":28800
}
您可以使用来调用PayPal Rest API。它为您处理所有授权和身份验证 下面是如何使用superagent获取访问令牌
superagent.post('https://api.sandbox.paypal.com/v1/oauth2/token')
.set("Accept","application/json")
.set("Accept-Language","en_US")
.set("content-type","application/x-www-form-urlencoded")
.auth("Your Client Id","Your Secret")
.send({"grant_type": "client_credentials"})
.then((res) => console.log("response",res.body))
此外,您还可以使用
axios
和async/await
:
const axios=require('axios');
(异步()=>{
试一试{
const{data:{access_token}}=wait axios({
网址:'https://api.sandbox.paypal.com/v1/oauth2/token',
方法:“post”,
标题:{
接受:'application/json',
“接受语言”:“en_US”,
“内容类型”:“应用程序/x-www-form-urlencoded”,
},
认证:{
用户名:客户端id,
密码:client_secret,
},
参数:{
授予类型:“客户端凭据”,
},
});
log('access_token:',access_token');
}捕获(e){
控制台错误(e);
}
})();
现代问题需要现代解决方案:
const fetch = require('node-fetch');
const authUrl = "https://api-m.sandbox.paypal.com/v1/oauth2/token";
const clientIdAndSecret = "CLIENT_ID:SECRET_CODE";
const base64 = Buffer.from(clientIdAndSecret).toString('base64')
fetch(authUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Accept-Language': 'en_US',
'Authorization': `Basic ${base64}`,
},
body: 'grant_type=client_credentials'
}).then(function(response) {
return response.json();
}).then(function(data) {
console.log(data.access_token);
}).catch(function() {
console.log("couldn't get auth token");
});
您所说的对大多数需求都是正确的,但对“支付体验/web配置文件”来说并非如此。同样,不是nodejs工程师,但是,我看到PayPal Node SDK也有示例,如果您仍然使用Node SDK发现问题,请告诉我。我们很乐意帮助您修复/更新,让您更快地集成paypal API。非常感谢您的回复!如何获得“access\u token”,我尝试过像这样的“body.access\u token”,但它返回未定义的@Dimitri
const{access\u token}=JSON.parse(body)代码>我们是否可以将其转换为本机fetch()?感谢您提供此解决方案。为了生成令牌,我从ajax迁移到axios。很好用。@Gediminas很好,我很高兴这有帮助。您还可以将此答案+1;)非常感谢,这个解决方案对我有效,因为我使用的是使用Axios的NestJS。@HartWoomÀla Perfecture。你是个救命恩人。几天来,我一直在努力进行PayPal/React和沙盒订阅测试。。。我可以让它在prod中工作,但不是沙盒,这是让它工作的第一步