AWSAccessKeyId未经授权

时间:2016-08-04 22:35:39

标签: ruby-on-rails-4 amazon-web-services mechanicalturk

我正在尝试使用GET请求来使用Amazon Mechanical Turk GetFileUploadURL函数。但是,我在尝试时会收到此错误代码。

AWS.NotAuthorized The identity contained in the request is not authorized to use this AWSAccessKeyId

这是我用来创建请求的代码。

    now = DateTime.now

    #For creating the signature hash
    data = "AWSMechanicalTurkRequesterGetFileUploadURL" + now.to_s
    sha256 = OpenSSL::Digest::SHA256.new
    sig = OpenSSL::HMAC.digest(sha256, Rails.configuration.secret_key, data)
    signature = Base64.encode64(sig)
    puts "https://mechanicalturk.amazonaws.com", "/?Service=AWSMechanicalTurkRequester&AWSAccessKeyId=#{Rails.configuration.aws_key}&Version=2014-08-15&Operation=GetFileUploadURL&Signature=#{sig}&Timestamp=#{now}&AssignmentId=#{mturk_results[0][:AssignmentId]}&QuestionIdentifier=file1"

    puts "\n\n"

    uri = URI('https://mechanicalturk.amazonaws.com')
    params = {:Service=>"AWSMechanicalTurkRequester", :AWSAccessKeyId=>Rails.configuration.aws_key, :Version=>"2014-08-15", :Operation=>"GetFileUPloadURL", :Signature=>sig, :Timestamp=>now, :AssignmentId=>mturk_results[0][:AssignmentId], :QuestionIdentifier=>"file1"}
    uri.query = URI.encode_www_form(params)
    res = 0
    Net::HTTP.start(uri.host, uri.port,
        :use_ssl => uri.scheme == 'https') do |http|
        request = Net::HTTP::Get.new uri

        res = http.request request # Net::HTTPResponse object
    end

关于我做错了什么的想法?

1 个答案:

答案 0 :(得分:1)

您需要正确格式化TimeStamp。时间戳必须采用UTC并采用以下ISO 8601格式: YYYYMMDD'T'HHMMSS'Z”。例如,20150830T123600Z是有效的时间戳。不要在时间戳中包含毫秒。

它必须是以下形式: 时间戳= 2016-04-23T08:00:05Z

Ruby的DateTime.now方法返回表单的形式: 2016-08-05T10:43:27-07:00

您可以在此处详细了解Timestamp和AWS签名: http://docs.aws.amazon.com/general/latest/gr/sigv4-date-handling.html