mardi 13 octobre 2015

aws web service call to api gateway

I'm trying to call the AWS API gateway resource /apikeys to get a list of my api keys. This isn't a part of the sdk yet so I'm trying to do the web service call in Ruby and I'm getting a 400. What I've read indicates I'm not setting up the headers and body correctly. Any help would be appreciated. I'm using the aws4 gem for signing the headers and the faraday gem to make the GET call. Here's the code & output.

#! /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" => "http://ift.tt/1LskQRe"
}
body="{}"

# sign headers
uri = URI("http://ift.tt/1LskOZK")
headers_signed = signer.sign("GET", uri, headers, body)
pp headers_signed

conn = Faraday.new(:url => "http://ift.tt/1LskQRe")

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

pp resp.status

Here's the output of the code.

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"=>"http://ift.tt/1LskQRe",
 "Authorization"=>
  "AWS4-HMAC-SHA256 Credential=AKIAI2P7SWBU6FBHPMOA/20151011/us-east-1/apigateway/aws4_request, SignedHeaders=content-type;date;host, Signature=85536b2084c11492d7b1af612b4f0a83752bb921e8534155dfbebeaefde6a73d"}
400




Aucun commentaire:

Enregistrer un commentaire