gpt4 book ai didi

javascript - 如何在 React Native fetch 中使用授权 header 来使用 Yelp API 发出获取请求

转载 作者:行者123 更新时间:2023-12-03 02:55:05 25 4
gpt4 key购买 nike

我正在尝试在react-native中使用FETCH发出发布请求。我收到验证错误。我在这里缺少什么?

    _fetchYelp(){

let data = {
method: 'POST',
body: JSON.stringify({
'client_id': 'id',
'client_secret': 'secret',
'grant_type': 'client_credentials'
}),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded'
}
}
return fetch('https://api.yelp.com/oauth2/token', data)
.then(response => response.json());

}


Error

status 400
timeout 0
response {"error":{"code":"VALIDATION_ERROR",
"description":"client_id or client_secret parameters not found. Make sure to provide client_id and client_secret in the body with the application/x-www-form-urlencoded content-type"}}
response_url https://api.yelp.com/oauth2/token

谢谢。

最佳答案

getAccessToken() {

let formData = new FormData();
formData.append('grant_type', 'client_credentials')
formData.append('client_id', 'yourID')
formData.append('client_secret', 'yourSecret')
let headers = new Headers();

return fetch('https://yourBaseUrl/oauth2/token/', {
method: 'POST',
headers: headers,
body: formData,
})
.then((response) => response.json())
}

关于javascript - 如何在 React Native fetch 中使用授权 header 来使用 Yelp API 发出获取请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47667610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com