Ios 使用curl命令进行react-native获取api

Ios 使用curl命令进行react-native获取api,ios,api,react-native,curl,fetch,Ios,Api,React Native,Curl,Fetch,我正在尝试为我正在创建的应用程序使用petfinder APi,并遵循APi文档,可以在此处找到: 它给出命令:curl-d“grant\u type=client\u凭证&client\u id={client-id}&client\u secret={client-secret}”https://api.petfinder.com/v2/oauth2/token 我正在尝试将其转换为react native,并使用了以下代码: getAdopt1 = async() => { fetc

我正在尝试为我正在创建的应用程序使用petfinder APi,并遵循APi文档,可以在此处找到:

它给出命令:
curl-d“grant\u type=client\u凭证&client\u id={client-id}&client\u secret={client-secret}”https://api.petfinder.com/v2/oauth2/token

我正在尝试将其转换为react native,并使用了以下代码:

getAdopt1 = async() => {
fetch('https://api.petfinder.com/v2/oauth2/token', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
firstParam: "grant_type=client_credentials&client_id={CLIENT-ID}&client_secret={CLIENT-SECRET}"
}),
}).then((response) => response.json())
.then((responseJson) => {
let res = JSON.stringify(responseJson)
console.log("Response: "+res)
return responseJson;
})
.catch((error) => {
console.error(error);
})
}
但是,我得到以下错误:

Response: {"type":"https://httpstatus.es/400","status":400,"title":"unsupported_grant_type","detail":"The authorization grant type is not supported by the authorization server.","errors":[{"code":"unsupported_grant_type","title":"Unauthorized","message":"The authorization grant type is not supported by the authorization server. - Check that all required parameters have been provided","details":"The authorization grant type is not supported by the authorization server. - Check that all required parameters have been provided","href":"http://developer.petfinder.com/v2/errors.html#unsupported_grant_type"}],"hint":"Check that all required parameters have been provided"}

我在这里做错了什么?

您正在发送一个JSON请求,但API期望的是一个表单数据请求

试试这样的方法:

var form=new FormData();
append('grant_type','client_credentials');
append('client_id','{client-id}');
append('client_secret','{client-secret}');
取('https://api.petfinder.com/v2/oauth2/token', {
方法:“POST”,
主体:形式,
})。然后(响应=>{
console.log(响应)
}).catch(错误=>{
控制台错误(error);
})

谢谢,我想这可能已经奏效了,但是有没有一种特殊的方式来打印响应,因为我得到了响应:响应{“{u bodyBlob”:Blob{“{u data”:Object{“blobId”:“598BFE2F-6337-4510-86A8-6B227B9BC000”,“name”:“token”,“offset”:0,“size”:853,“type”:“application/json”,},}对象{“blobId”:“598BFE2F-6337-4510-86A8-6B227B9BC000”,“name”:“token”,“offset”:0,“size”:853,“type”:“application/json”,},},而不是响应:{“token_type”:“Bearer”,“expires_in”:3600,“access_token”:“…”文档说明我应该使用
response.json()
response.text()
,具体取决于响应的格式。如果答案正确,请接受。