【问题标题】:How to use Authorization Header in React Native fetch to make a get request using Yelp API如何在 React Native fetch 中使用 Authorization Header 使用 Yelp API 发出获取请求
【发布时间】:2017-12-06 05:49:01
【问题描述】:

我正在尝试在 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

谢谢。

【问题讨论】:

    标签: javascript react-native fetch fetch-api yelp


    【解决方案1】:
    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())      
    }
    

    【讨论】:

      猜你喜欢
      • 2012-11-19
      • 1970-01-01
      • 2019-09-09
      • 2016-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-18
      相关资源
      最近更新 更多