使用Rails通过SSL测试第三方API

时间:2015-11-18 14:31:07

标签: ruby-on-rails ssl

我正在与第三方API集成,他们的文档包含以下信息:

仅允许通过SSL连接的请求。 api。{domainname} .com的G2 SSL证书由Go Daddy签名。在撰写本文时,证书属性为:

Certificate
   Version 3
   Serial Number:  04 14 56 13 ea b3 4a
   Signature Algorithm: sha256RSA
   Issuer:

C = US, S = Arizona, L = Scottsdale, O = GoDaddy.com, Inc., OU = http://certs.godaddy.com/repository/, CN = Go Daddy Secure Certificate Authority - G2

   Validity:
      From:  2015-March-03 03:09:50 GMT
      To:    2016-March-03 03:09:50 GMT

本文档中包含指向以下页面的链接:

https://certs.godaddy.com/repository

我的问题是,我正在努力与API集成并开发代码,如何获取证书并安装它以便我可以测试我的代码?这是使用Webrick的标准Rails开发环境。

1 个答案:

答案 0 :(得分:1)

据我所知(http://www.shiprightsolutions.com/developers.html),API要求请求通过SSL,这只表示网址将以https://开头,您不需要安装任何证书来发出请求。 虽然,它使用API​​密钥来识别客户端(您的应用程序),因此您必须将其与请求一起传递到他们称为SRS-ApiKey的自定义标头中。在文档中 你指出,他们说你必须向他们发送一封电子邮件,要求使用API​​密钥进行测试。

获得密钥后,您可以使用cURL测试请求。包含样本请求和回复的详细信息可以在此处下载(http://www.shiprightsolutions.com/documents/SRS-API-093015.zip

curl -H "SRS-ApiKey: YOUR API KEY" -H "Content-Type: application/json" -X POST - d@scenario1-request.json https://apiplay.shiprightsolutions.com/v1/order -v 

在您的rails应用中,您可以使用Faraday(https://github.com/lostisland/faraday)等HTTP客户端库。

require 'faraday'

conn = Faraday.new(:url => 'https://apiplay.shiprightsolutions.com/v1') do |faraday|
    faraday.response :logger
    faraday.headers['SRS-ApiKey'] = 'XXX'
    faraday.adapter Faraday.default_adapter    # make requests with Net::HTTP
end

response = conn.post do |req|
    req.url '/order'
    req.headers['Content-Type'] = 'application/json'
    req.body = '{ "id": 1, "param": "value" }'
end

puts response.body