【问题标题】:Missing parameter client_id?? Ruby RestClient issues缺少参数client_id?? Ruby RestClient 问题
【发布时间】:2016-11-07 21:08:06
【问题描述】:

我正在使用 RestClient 来处理 Spotify API(但我假设这是一个普遍的问题,而不是专门的 spotify)。

API 需要传递 client_id。我的代码: 授权 = Base64.strict_encode64 "#{@clientID}:#{@clientSecret}" 开始

request_body = { response_type: 'code', client_id: @clientID, redirect_uri: REDIRECT_URI }
#response = RestClient.get(AUTHORIZE_URI, request_body)
puts response = RestClient::Request.execute(method: :get, url: AUTHORIZE_URI, payload: request_body)

救援 RestClient::BadRequest => e puts e.response 结束

但是我收到了一个 BadRequest 异常,并且响应状态为“缺少必需的参数:client_id”。

如果我卷曲:

puts `curl -I -s -X GET "#{AUTHORIZE_URI}?client_id=#{@clientID}&response_type=code&redirect_uri=REDIRECT_URI"`

我得到一个正常的 200 OK 响应。这是怎么回事???

【问题讨论】:

    标签: ruby rest-client


    【解决方案1】:

    您必须使用 headers 散列中的 params 键来传递查询参数。下面的文档

    由于原始 API 中的选择很不幸,用于 填充查询字符串实际上是从标头哈希中取出的。 因此,如果您想同时传递参数哈希和更复杂的选项, 在标头哈希中使用特殊键 :params 。这种设计可能 在未来的主要版本中进行更改。

    RestClient::Request.execute(method: :get, 
                                url: 'http://example.com/resource',
                                timeout: 10, 
                                headers: {params: {foo: 'bar'}}
                               )
    
     ➔ GET http://example.com/resource?foo=bar
    

    【讨论】:

      猜你喜欢
      • 2021-11-05
      • 1970-01-01
      • 2014-05-31
      • 2017-01-15
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 2017-12-09
      • 2014-06-28
      相关资源
      最近更新 更多