我有一个从贝宝获取令牌的API。
curl -v POST https://api.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "CLIENT_ID:SECRET" \
-d "grant_type=client_credentials"
我正在使用axios进行调用,就像这样,但我在身份验证上遇到了错误。
axios.post("https://api.sandbox.paypal.com/v1/oauth2/token", {}, {
auth: {
username: "xxxx, // clientId
password: "xxxx" // client Secret
}
}).then(function(response) {
result = response;
console.log('Authenticated' + response);
}).catch(function(error) {
console.log('Error on Authentication' + error);
});
也许我漏掉了像“grant_type”这样的东西。如何在此调用中传递这些参数。请改正一下好吗?非常感谢你
发布于 2020-12-09 09:41:35
需要对${clientId}:${secretKey}
进行base64编码
在JavaScript中,您可以使用btoa()、toString('base64'),或者使用axios设置身份验证密钥。
对于curl,您可以使用-u命令行参数。
对于一般的rest API用法,当以这种方式将基本身份验证传递给oauth2/token端点时,查询字符串应该是grant_type=client_credentials,如documented here。这将返回您可以在所有其他access_token调用中使用的API。
您链接到的“连接到PayPal”文档是针对该特定集成的,您需要一个授权码,但其他任何应用程序接口都不使用该授权码。
发布于 2020-12-09 09:49:41
axios({
method: 'POST',
url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
headers: {
'Authorization': `Basic ${clientId}:${secretKey}`,
'Content-type': 'application/json'
},
data: `grant_type=authorization_code&code=${authorizationCode}`
})
发布于 2020-12-09 10:05:17
客户端ID和客户端密码不能是您的用户名和密码。在curl中,-u标志用于用户名和密码,但在axios中,这一点可能有所不同。对于Curl knowledge follow this。每件事都有一些选择,如果你很努力,那么你可以去这个库。PayPal-node-SDK。或者在youtube上学习一些教程。
https://stackoverflow.com/questions/65213992
复制相似问题