如何从 Skyscanner API 发布请求中检索会话密钥 - Ruby [英] How to retrieve Session key from Skyscanner API post request - Ruby
问题描述
在我的应用程序中,我想通过航班详细信息获取实时价格为此我使用了 SkyScanner API.在获得数据之前我已经阅读了文档我必须创建实时定价服务会话.可以通过向 api 发送请求创建它,然后它通过使用这个 SessionKey
和 apiKey
提供 SessionKey
我可以检索数据.那么我如何才能获得 Sessionkey,因为我理解它必须由 API Server 提供.
In my app I want to fetch Live Price by Flight Details for this I have used SkyScanner API. I have read the documentation before obtained data I have to create Live Pricing Service Session. Which can be created by post request to api and then it provide SessionKey
by using this SessionKey
and apiKey
I can retrived the data. So How can I get Sessionkey as I understood it must be provided by API Server.
这是我的尝试:
require 'json'
require 'net/http'
require 'uri'
post_params = {
:apiKey => "[API_KEY]",
:country => "GB",
:currency => "GBP",
:locale => "en-GB",
:adults =>1,
:children => 0,
:infants => 0,
:originplace => '11235',
:destinationplace => '13554',
:outbounddate => '2015-05-19',
:inbounddate => '2015-05-26',
:locationschema => 'Default',
:cabinclass => 'Economy',
:groupPricing => true
}
sessionkey_request = Net::HTTP.post_form(URI.parse('http://partners.api.skyscanner.net/apiservices/pricing/v1.0'), post_params )
get_data= "http://partners.api.skyscanner.net/apiservices/pricing/v1.0/?apiKey=[API_KEY]"
puts sessionkey_request.inspect
temp = Net::HTTP.get_response(URI.parse(get_data)).body
# puts temp
在控制台我得到
<Net::HTTPCreated 201 Created readbody=true> # sessionkey_request.inspect
没有它就无法获得 SessionKey 响应 我无法检索数据.请指导我哪里出错了.感谢您的解决.
Not getting SessionKey in response without it I can not retrieved data. Please guide me where I do mistake. I appreciate for solution.
了解更多详情和实时结果查看演示 通过 API
Fore more details and live result Check Demo by API
注意:我已经检查了 gem 'skyscanner' 但它不提供任何实时价格的方法.它提供了浏览缓存方法.
Note: I have check gem 'skyscanner ' but it doesn't provide any method for Live Price. It provide Browse Cache methods.
推荐答案
根据文档:
成功的响应不包含任何内容.轮询预订的 URL详细信息在响应的 Location 标头中指定
A successful response contains no content. The URL to poll the booking details is specified in the Location header of the response
试试这个:
sessionkey_request["location"]
我已经在我的系统上对其进行了测试,它返回给我:
I have test it on my system and it returns me:
http://partners.api.skyscanner.net/apiservices/pricing/v1.0/8e28260becd3441ca4e865396e224e7d_ecilpojl_EC71481935CEBB7EAF661BC24940D01D
最后一部分是您的 sessionKey
,您可以将其用于 GET
请求.如果您只想要最后一部分(sessionKey),那么您可以通过以下方式检索它:
last part is your sessionKey
which you can use for GET
request. If you want only last part(sessionKey) then you can retrieved it by:
> url = "http://partners.api.skyscanner.net/apiservices/pricing/v1.0/8e28260becd3441ca4e865396e224e7d_ecilpojl_EC71481935CEBB7EAF661BC24940D01D"
> url.split('/').last
=> "8e28260becd3441ca4e865396e224e7d_ecilpojl_EC71481935CEBB7EAF661BC24940D01D"
这篇关于如何从 Skyscanner API 发布请求中检索会话密钥 - Ruby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!