【发布时间】:2021-11-14 12:23:57
【问题描述】:
我正在尝试连接到名为 PayU 的支付 api,他们提供了如何通过 curl 连接的示例,一个是通过 rest api 连接
curl -X POST https://secure.payu.com/pl/standard/user/oauth/authorize \
-d 'grant_type=client_credentials&client_id=145227&client_secret=12f071174cb7eb79d4aac5bc2f07563f'
还有一个是通过我也想使用的 SDK 进行连接,但是这个需要在商店中进行额外设置,而且我在第一个方面遇到了问题,所以如果有人愿意破译另一个也会很棒
curl -X POST https://secure.payu.com/pl/standard/user/oauth/authorize \
-H "Cache-Control: no-cache"
-H "Content-Type: application/x-www-form-urlencoded"
-d 'grant_type=trusted_merchant&client_id=[provided by PayU]&client_secret=[provided by PayU]&email=[users email]&ext_customer_id=[Id of the customer used in merchant system]'
在 curl 中,第一个传递令牌没有问题,但是我试图在代码中做同样的事情,但我做不到。这是我的代码:
fetch('https://secure.payu.com/pl/standard/user/oauth/authorize', {
method: 'POST',
body: JSON.stringify({
'grant_type': 'client_credentials',
'client_id': '145227',
'client_secret': '12f071174cb7eb79d4aac5bc2f07563f',
})
}).then(res => {
if (!res.ok) {
throw new Error("Fetching payU failed, please try again later!");
}
return res;
})
.then(data => {
console.log(data)
return { payUdata: data }
})
.catch(err => {
console.log(err);
});
【问题讨论】:
-
要添加到@Skarlinski,请将
body: JSON.stringify(...)替换为body: new URLSearchParams(Object.entries({your_object})).toString();。此外,在您的选项中添加标题以指定内容类型:headers: {"content-type": "application/x-www-url-encoded"}
标签: javascript curl fetch fetch-api