缺少参数 client_id?? Ruby RestClient 问题

Missing parameter client_id?? Ruby RestClient issues

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

API需要通过client_id。我的代码: authorization = 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 放 e.response 结束

但是我遇到了 BadRequest 异常,并且响应状态为 "Missing required parameter: client_id"。

如果我卷曲:

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

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

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

Due to unfortunate choices in the original API, the params used to populate the query string are actually taken out of the headers hash. So if you want to pass both the params hash and more complex options, use the special key :params in the headers hash. This design may change in a future major release.

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

 ➔ GET http://example.com/resource?foo=bar