我正在嘗試將用于 KYC 流程的 mati api 集成到我的 React 原生應用程式中。為了獲得身份驗證令牌,我參考了下面的檔案。 https://docs.getmati.com/reference/authentication 在郵遞員中測驗作業正常并得到了 auth_token 的回應。然而,在 React Native 代碼中,它得到了 400 錯誤。請幫我解決這個問題。代碼片段:
const myoptions = {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
Authorization:
'Basic AAAAAAAAAAAAAAAAAAAAAAAAAAA',
},
body: new URLSearchParams({grant_type: 'client_credentials'}),
};
fetch('https://api.getmati.com/oauth', myoptions)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
錯誤是:
LOG {"code": 400, "message": "Missing parameter: `grant_type`", "name": "invalid_request", "status": 400, "statusCode": 400}
uj5u.com熱心網友回復:
從您提到的檔案來看,它似乎正在接受表單資料,并且您正在發送搜索引數。嘗試這個
const formData = new FormData();
formData.append("grant_type","client_credentials");
const myoptions = {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
Authorization:
'Basic AAAAAAAAAAAAAAAAAAAAAAAAAAA',
},
body: formData,
};
fetch('https://api.getmati.com/oauth', myoptions)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
uj5u.com熱心網友回復:
我發現了錯誤并想通如下。
const body = new URLSearchParams({grant_type: 'client_credentials'});
const myoptions = {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
Authorization:
'Basic AAAAAAAAAAAAAAAAAAAAAAAAAAA',
},
body: body.toString(),
};
fetch('https://api.getmati.com/oauth', myoptions)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
在 react native 中,我們必須將 body 作為字串發送,它運行良好并得到正確的回應。謝謝你。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/365449.html
標籤:javascript 反应 反应原生 接口
上一篇:在提交按鈕上反應原生表單處理
下一篇:如何從子組件獲取值?
