【问题标题】:Zomato api url request returning error saying invalid api keyZomato api url 请求返回错误说无效的 api 密钥
【发布时间】:2018-07-01 23:52:54
【问题描述】:

我正在尝试获取存储在 zomato api 中的数据并检查网站中生成的一些 url,例如,

<a href="https://developers.zomato.com/api/v2.1/restaurant?res_id=RESID">

https://developers.zomato.com/api/v2.1/restaurant?res_id=RESID,

我收到一条错误消息:

{"code":403,"status":"Forbidden","message":"Invalid API Key"}

虽然这个相同的 url 给出了有关某些餐厅的数据的 json 格式响应。另外,我生成了一个 zomato API 密钥。但是,问题。 请让我知道我错在哪里,以便我纠正自己。

谢谢。

【问题讨论】:

    标签: zomato-api


    【解决方案1】:

    尝试从 http 请求的头部传递 Zomato api 密钥,而不是在查询字符串中传递密钥

    【讨论】:

      【解决方案2】:

      如果您尝试获取 API 以获取响应数据。

      通过标头传递 API 密钥

      axios({
            method: "GET",
            url: "https://developers.zomato.com/api/v2.1/search",
            headers: {
              "user-key": "API_KEY",
              "content-type": "application/json"
            }
          })
            .then(response => {
              console.log(response.data.restaurants[0].restaurant.name);
            })
            .catch(error => {
              console.log(error);
            });
      

      【讨论】:

        【解决方案3】:

        在 javascript 结构中,api 以这种方式调用:

        $.ajax({  
        url: "https://developers.zomato.com/api/v2.1/restaurant?res_id=RESID",
        dataType: 'json',
        async: true,
        beforeSend: function(xhr){xhr.setRequestHeader('user-key', 
        'INSERT_API_KEY_HERE');},  // This inserts the api key into the HTTP header
        success: function(response) { console.log(response) } });
        

        【讨论】:

          【解决方案4】:
           public Map < String, String > getHeaders() {
              Map < String,
              String > params = new HashMap < String,
              String > ();
              params.put("user-key", "xxx");
              params.put("Accept", "application/json");
              return params;
            }
          

          【讨论】:

          • 虽然只有代码的答案可能对提问者有所帮助,但社区感谢您的解释。您能否详细说明为什么以及如何工作?
          猜你喜欢
          • 2020-10-13
          • 2014-08-05
          • 2018-07-05
          • 2015-07-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-30
          相关资源
          最近更新 更多