文档说获取oauth
令牌使用以下内容:
curl -v https://api.sandbox.paypal.com/v1/oauth2/token \ -H“Accept:application / json”\ -H“Accept-Language:en_US”\ -u“EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp”\ _d“grant_type = client_credentials”
如何使用-u
发送-d
和angular js
参数
var senddata = {
clientId:"Ac8T1BCmIxgxbS5O_ztwejxvNW8Cbr0y1724_2cd8bGl68axHyw_nEdcOBli",
secret:"EAwgXhALV78LnphCPf-R3zs1Dx3nkcIOMc4TftSLh9q5EpgyqdeE19El9Oh0",
grant_type:"client_credentials"
};
试图在senddata
(http.post
)传球url,senddata
但我得到了
{"error":"invalid_client","error_description":"Invalid client credentials"}
我正在使用Angular-JS开发的电子商务解决方案中进行PayPal API调用。我能够使用以下代码从PayPal获取auth_token。吸引我一段时间的两个陷阱是:
data: {'grant_type': 'client_credentials'}
发送时,我收到的错误是“grant_type”是必需参数。将此更改为使用等号连接的字符串后,我不再收到此错误。
var basicAuthString = btoa('CLIENTID:SECRET');
$http({
method: 'POST',
url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + basicAuthString,
},
data: 'grant_type=client_credentials'
})
.success(function(data){
console.log(data.access_token);
})
.error(function(error){
console.error(error);
});
我希望这对那些试图实现这一点的人有所帮助。
客户端ID和密码需要在Authorization
标头中发送。
客户端ID和密码需要与:
分隔符(例如client:secret
)连接,然后在base 64中编码。这称为basic authentication,以及使用-u
选项时cURL在场景后的作用。
您可以在第三个参数中将自定义标头传递给$http.post
。见method documentation。例如,直接使用$http
(并且从文档中获得了很大的启发):
var req = {
method: 'POST',
url: 'http://example.com',
headers: {
'Authorization': "Basic 12Base346456enc0did"
},
data: { grant_type: 'client_credentials' }
}
$http(req).success(function(){...}).error(function(){...});