aws web服务调用api网关

时间:2015-10-13 15:03:34

标签: ruby-on-rails ruby amazon-web-services aws-sdk

我正在尝试调用AWS API网关资源/ apikeys来获取我的api密钥列表。这不是sdk的一部分,所以我试图在Ruby中进行Web服务调用,我得到400.我读过的内容表明我没有正确设置标题和正文。任何帮助,将不胜感激。我正在使用aws4 gem来签署标头和法拉第宝石以进行GET调用。这是代码&输出。

#! /home/sburke/.rvm/rubies/ruby-2.2.3/bin/ruby

require 'aws4'
require 'pp'
require 'faraday'
require 'json'
require 'time'

# create a signer
signer = AWS4::Signer.new(
  access_key: ENV['AWS_ACCESS_KEY_ID'],
  secret_key: ENV['AWS_SECRET_ACCESS_KEY'],
  region: "us-east-1"
)

# build request
headers = {
  "Date" => Time.now.httpdate,
  "Content-Type" => "application/x-amz-json-1.0",
  "host" => "https://apigateway.us-east-1.amazonaws.com"
}
body="{}"

# sign headers
uri = URI("https://apigateway.us-east-1.amazonaws.com/apikeys")
headers_signed = signer.sign("GET", uri, headers, body)
pp headers_signed

conn = Faraday.new(:url => "https://apigateway.us-east-1.amazonaws.com")

resp = conn.get do |req|
  req.url '/apikeys'
  req.headers = headers_signed
end

pp resp.status

这是代码的输出。

sburke@sburke-laptop:~/sandbox/aws_rest_ruby$ ./test.rb 
Digest::Digest is deprecated; use Digest
Digest::Digest is deprecated; use Digest
Digest::Digest is deprecated; use Digest
Digest::Digest is deprecated; use Digest
Digest::Digest is deprecated; use Digest
{"Date"=>"Sun, 11 Oct 2015 02:14:13 GMT",
 "Content-Type"=>"application/x-amz-json-1.0",
 "host"=>"https://apigateway.us-east-1.amazonaws.com",
 "Authorization"=>
  "AWS4-HMAC-SHA256 Credential=AKIAI2P7SWBU6FBHPMOA/20151011/us-east-1/apigateway/aws4_request, SignedHeaders=content-type;date;host, Signature=85536b2084c11492d7b1af612b4f0a83752bb921e8534155dfbebeaefde6a73d"}
400

这是标题的输出,正文是空的。除了400和标题之外,我没有从法拉第响应对象获得任何更多信息。

pp resp.headers
pp resp.body
{"transfer-encoding"=>"chunked",
 "date"=>"Wed, 14 Oct 2015 17:27:25 GMT",
 "connection"=>"close"}
""

0 个答案:

没有答案