[英]Paypal API with angular | 400 (Bad Request)
I'm working with paypal API and flowing with this document make your firest call . 我正在使用贝宝(Paypal)API,并且正在使用此文档进行最紧急的呼叫 。
I got a Status Code 400 (Bad Request) with an error message: 我收到状态代码400(错误请求),并显示一条错误消息:
Object {error: "invalid_request", error_description: "grant_type is a required parameter"} 对象{错误:“ invalid_request”,错误描述:“ grant_type是必需参数”}
'Authorization': "Basic RU9KMlMtWjZPb05fbGVfS1MxZDc1d3NaNnkwU0ZkVnNZOTE4M0l2eEZ5WnA6RUNsdXNNRVVrOGU5aWhJN1pkVkxGNWNaNnkwU0ZkVnNZOTE4M0l2eEZ5WnA=",
In this case I used the Client-Id and Secret which provided by the example and encoded them in base 64. 在这种情况下,我使用示例提供的Client-Id和Secret并将它们编码为base 64。
$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)
});
The root cause is your data is encoded as JSON instead of application/x-www-form-urlencoded
, so what you need to do is URL-encode data like: 根本原因是您的数据编码为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'}
})
You'll need to include the 'withCredentials' option. 您需要包括“ withCredentials”选项。 Make sure that you put a colon in between your client-id and your secret key. 确保在客户端ID和密钥之间插入一个冒号。 You shouldn't need to encode those values at all, just use the ones Paypal gives you. 您根本不需要编码这些值,只需使用Paypal给您的值即可。 I also think the data field should not be an object, as Paypal seems to be expecting a string. 我还认为数据字段不应是对象,因为Paypal似乎期望使用字符串。
If you are not using windows, I believe you'll need to set the content type to JSON, but the documentation is a bit vague there. 如果您不使用Windows,我相信您需要将内容类型设置为JSON,但是那里的文档有点含糊。 Maybe try both? 也许两者都尝试? I think this request config should work: 我认为这个请求配置应该工作:
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'
Lastly, you may need to configure your $httpProvider. 最后,您可能需要配置$ httpProvider。 I've never had to, but I'm unsure of your environment and Angular version: 我从不需要,但是我不确定您的环境和Angular版本:
$httpProvider.defaults.useXDomain = true;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.