【问题标题】:Rails External Api CallRails 外部 API 调用
【发布时间】:2018-06-26 23:31:44
【问题描述】:

如何使用带有 api 令牌的 rest-client gem 调用外部 api?

我正在做这样的事情

RestClient.get('api.openweathermap.org/data/2.5/weather?id=2172797', headers={appid: 'xxxxxxxxxxxxxxxxxxxxxxx'})

但这给了我未经授权的回应。令牌标头的正确名称是什么? Open weather api

我想不通。

【问题讨论】:

  • 看起来你可以将它附加到 URI 如果你在紧要关头。 http://api.openweathermap.org/data/2.5/weather?id=2172797&appid=xxxxxxxxx

标签: ruby-on-rails api rest-client


【解决方案1】:

尝试使用字符串插值将API_TOKEN 放入url,使其看起来像这样。

http://api.openweathermap.org/data/2.5/forecast?id=524901&APPID=1111111111

所以:

RestClient.get("api.openweathermap.org/data/2.5/weather?id=2172797&APPID=#{API_TOKEN}")

【讨论】:

    【解决方案2】:

    我应该将 APPID 放在 params 哈希中。 所以我找到的最好的解决方案是创建 ENV 变量并在我的文件中使用它。然后我就可以打电话了。

    RestClient.get 'http://api.openweathermap.org/data/2.5/weather?q=London', {params: {APPID: API_KEY}}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-16
      • 2020-10-05
      相关资源
      最近更新 更多