Angularjs Paypal API,角度| 400(请求错误)
我正在使用paypal API,并正在使用此文档 我收到一个状态代码400(错误请求),并显示一条错误消息: 对象{错误:“无效的\u请求”,错误\u描述:“grant\u类型是必需的参数”} 在本例中,我使用了示例提供的客户机Id和Secret,并将它们编码在base 64中Angularjs Paypal API,角度| 400(请求错误),angularjs,paypal,Angularjs,Paypal,我正在使用paypal API,并正在使用此文档 我收到一个状态代码400(错误请求),并显示一条错误消息: 对象{错误:“无效的\u请求”,错误\u描述:“grant\u类型是必需的参数”} 在本例中,我使用了示例提供的客户机Id和Secret,并将它们编码在base 64中 $http({ url: 'https://api.sandbox.paypal.com/v1/oauth2/token', method: 'POST',
$http({
url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': "Basic RU9KMlMtWjZPb05fbGVfS1MxZDc1d3NaNnkwU0ZkVnNZOTE4M0l2eEZ5WnA6RUNsdXNNRVVrOGU5aWhJN1pkVkxGNWNaNnkwU0ZkVnNZOTE4M0l2eEZ5WnA=",
'Accept-Language': 'en_US'
},
data: { grant_type: 'client_credentials' }
}).success(function (data, status, headers, config) {
console.log(data)
}).error(function (data, status, headers, config) {
console.log(data)
});
您需要包括“withCredentials”选项。确保在客户端id和密钥之间加上冒号。您根本不需要对这些值进行编码,只需使用Paypal提供的值即可。我还认为数据字段不应该是一个对象,因为Paypal似乎需要一个字符串 如果您不使用windows,我相信您需要将内容类型设置为JSON,但是文档有点模糊。也许两种都试试?我认为这个请求配置应该可以工作:
url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
method: 'POST',
withCredentials: true,
headers: {'Content-Type': 'application/json',
'Authorization': 'Basic {Client-Id}:{Secret}',
'Accept-Language': 'en_US'
},
data: 'grant_type=client_credentials'
最后,您可能需要配置$httpProvider。我从来没有这样做过,但我不确定您的环境和角度:
$httpProvider.defaults.useXDomain = true;
根本原因是您的数据被编码为JSON,而不是
application/x-www-form-urlencoded
,因此您需要做的是URL编码数据,如:
$http({
method: 'POST',
url: url,
data: $.param({grant_type: 'client_credentials'}),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
那么你的问题是什么?
$http({
method: 'POST',
url: url,
data: $.param({grant_type: 'client_credentials'}),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})