我正在尝试向Spingo接口发出请求。我已经使用Curl成功地提出了一个请求,但我正在尝试使用Faraday gem。
成功的curl请求如下所示:
curl -v --url "calendarapi.spingo.com/v1/events?sections=42336" --header 'Authorization: SpingoAPI token="my-api-key"'最初,我尝试使用默认适配器net-http。然后,我切换到Typheous适配器,因为net-http在Authorization中将我的SpingoAPI方案大写。这使得它可以读取Spingoapi。现在,我的请求如下所示:
def make_request
conn = Faraday.new(base_url) do |f|
f.params['sections'] = '42336'
f.adapter :typhoeus
f.use Faraday::Response::Logger
end
conn.authorization(:SpingoAPI, token: api_key)
conn.get
end切换到Typheous允许返回一些东西(我没有从net-http得到任何东西),所以我的响应如下所示。
ETHON: performed EASY effective_url=url
sections=42336 response_code=200 return_code=ok total_time=0.844381
I, [2015-08-05T11:16:06.575611 #22692] INFO -- : get http://www.calendarapi.spingo.com/v1/events?sections=42336
D, [2015-08-05T11:16:06.575842 #22692] DEBUG -- request: User-Agent: "Faraday v0.9.1"
Authorization: "SpingoAPI token=\"my-token\""
I, [2015-08-05T11:16:06.576081 #22692] INFO -- Status: 200
D, [2015-08-05T11:16:06.576230 #22692] DEBUG -- response: Content-Type: "text/html; charset=utf-8"
Date: "Wed, 05 Aug 2015 17:16:03 GMT"
Server: "nginx/1.2.9"
Set-Cookie: "VisitorID=GsPJ8NLBiP; expires=Tue, 03-Nov-2015 17:16:03 GMT"
X-Powered-By: "PHP/5.3.10-1ubuntu3.11"
Content-Length: "152"
Connection: "keep-alive"这让我相信我没有被授权,但是我不知道如何以不同的方式传递授权。
任何帮助都将不胜感激。
发布于 2018-08-02 19:41:15
试试这个:
# set variables
base_url = "calendarapi.spingo.com"
url = "/v1/events"
token = "SpingoAPI token='my-api-key'"
params = {:sections => 42336}
# init connection object
connection = Faraday.new(:url => base_url) do |c|
c.use Faraday::Request::UrlEncoded
c.use Faraday::Response::Logger
c.use Faraday::Adapter::NetHttp
end
# send request
response = connection.get url, params do |request|
request.headers["Authorization"] = token
endhttps://stackoverflow.com/questions/31839319
复制相似问题