简体   繁体   English

如何在React Native访存中使用授权标头使用Yelp API进行获取请求

[英]How to use Authorization Header in React Native fetch to make a get request using Yelp API

I am trying to make a post request using FETCH in react-native. 我试图在react-native中使用FETCH发出发布请求。 I am getting a validation error. 我收到验证错误。 What am I missing here? 我在这里想念什么?

    _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

Thank you. 谢谢。

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())      
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM