AWS API authentication in R : Missing Authenticati

2019-03-06 00:38发布

问题:

I need to authenticate AWS API in R. I tried using aws.signature package to do the same and I am getting 403 response with error Missing Authentication Token . It seems that I am missing some necessary parameters. Looking for assistance to debug the below code or ways to authenticate AWS API in R.

# To create aws signature for authentication for the rest API call

library(aws.signature)
library(httr)

# validate arguments and setup request URL
current <- Sys.time()
d_timestamp <- format(current, "%Y%m%dT%H%M%SZ", tz = "UTC")

hdrs <- list(`Content-Type` = "application/x-www-form-urlencoded",
         Host = "jteti5wnje.execute-api.eu-central-1.amazonaws.com",
         `x-amz-date` = d_timestamp)

params <- signature_v4_auth(
datetime = d_timestamp,
              region = "eu-central-1",
              service = "execute-api",
              verb = "GET",
              action = "iMetaAPI",
              query_args = list(),
              canonical_headers = hdrs,
              request_body = "json",
              key = "***************",
              secret = "*****************",
              session_token = NULL,
              query = FALSE,
              algorithm = "AWS4-HMAC-SHA256",
              verbose = TRUE)


a <- GET("https://jteti5wnje.execute-api.eu-central-1.amazonaws.com/iMetaAPI",
query = params)

rawToChar(a$content)

回答1:

A few things:

  • request_body needs to be the actual body. You're doing a GET() so it should just be NULL or "".
  • The response from signature_v4_auth() is a list but you don't need all of its elements - probably just the hds$Authorization <- params$SignatureHeader element.
  • The actual errors you're getting is because you haven't passed the headers. You need to pass the headers to GET() with something like: do.call(add_headers, hdrs) so you can do:

    a <- GET("https://jteti5wnje.execute-api.eu-central-1.amazonaws.com/iMetaAPI", do.call(add_headers, headers))
    

That will probably work, or you'll at least get a more informative error.