为了确认付款,我需要一个每 8 小时过期一次的 access_token,因此我需要在尝试验证付款之前使用云代码获取新的访问 token 。
我正在尝试使用 httpRequest:
function requestAccessToken(response){
Parse.Cloud.httpRequest({
method: 'POST',
url: 'https://api.paypal.com/v1/oauth2/token',
headers: {
'Content-Type': 'application/json',
'Accept-Language' : 'en_US'
},
body: 'grant_type=client_credentials',
success: function(httpResponse) {
var res = JSON.parse(httpResponse.text);
response.success(res.access_token);
},
error: function(httpResponse) {
response.error('requestAccessToken failed with response code ' + httpResponse.status);
}
});
}
要执行以下 curl ( from docs ):
curl -v 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"
问题是我找不到将“{client-ID}:{secret}”添加到 httpRequest 的方法。
使用 url: 'https://'+clientId+':'+secret+'@api.paypal.com/v1/oauth2/token'
导致 500 服务器响应。
关于如何获取访问 token 以验证付款的任何想法?
最佳答案
您需要添加授权 header
Authorization: Basic < {client-ID}:{secret}" encoded base64> (e.g. QWxhZGRpbjpvcGVuIHNlc2FtZQ==)
关于curl - 使用 Parse.com 云代码确认 PayPal 付款,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25964650/