简体   繁体   English

法拉第授权标头

[英]Faraday Authorization Header

I am trying to make a request to the Spingo API. 我正在尝试向Spingo API发出请求。 I have made a successful request using Curl, but am trying to use the Faraday gem. 我已经使用Curl发出了成功的请求,但是正在尝试使用Faraday gem。

The successful curl request looks like this: 成功的curl请求如下所示:

curl -v --url "calendarapi.spingo.com/v1/events?sections=42336"  --header 'Authorization: SpingoAPI token="my-api-key"'

Inititially I tried tried using the default adapter net-http . 最初,我尝试使用默认适配器net-http尝试。 Then I switched to the Typheous adapter because net-http was capitalizing my SpingoAPI scheme in the Authorization header. 然后我切换到Typheous适配器,因为net-http在Authorization标头SpingoAPI我的SpingoAPI方案大写。 This made it read Spingoapi. 这使其成为Spingoapi。 Now my request looks like this: 现在我的请求看起来像这样:

  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

Switching to Typheous allowed something to return (I wasn't getting anything back from net-http ) So my response looks like this. 切换到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"

This leads me to believe that I am not being authorized, however I am out of ideas on how to pass in the authorization differently. 这使我相信自己没有得到授权,但是我对如何以不同方式传递授权没有想法。

Any help would be greatly appreciated. 任何帮助将不胜感激。

try this : 尝试这个 :

# 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
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM