Class: PaapiMini::Auth
- Inherits:
-
Object
- Object
- PaapiMini::Auth
- Defined in:
- lib/aws_v4_auth.rb
Constant Summary collapse
- HMAC_ALGORITHM =
'AWS4-HMAC-SHA256'
- AWS4_REQUEST =
'aws4_request'
Instance Attribute Summary collapse
-
#access_key ⇒ Object
readonly
Returns the value of attribute access_key.
-
#current_date ⇒ Object
readonly
Returns the value of attribute current_date.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#http_method_name ⇒ Object
readonly
Returns the value of attribute http_method_name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#region ⇒ Object
readonly
Returns the value of attribute region.
-
#secret_key ⇒ Object
readonly
Returns the value of attribute secret_key.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
-
#signed_headers ⇒ Object
readonly
Returns the value of attribute signed_headers.
-
#xamz_date ⇒ Object
readonly
Returns the value of attribute xamz_date.
Class Method Summary collapse
- .create_search_items(access_key, secret_key, partner_tag, keywords:, item_page: 1, min_price: 1, max_price: 100000, min_review_rating: 1, host: "webservices.amazon.co.jp", region: "us-west-2", market_place: "www.amazon.co.jp") ⇒ Object
- .hmac_sha_256(key, base_string) ⇒ Object
- .utc_timestamp(time) ⇒ Object
Instance Method Summary collapse
- #build_authorization_string(signature) ⇒ Object
- #calculate_signature(string_to_sign) ⇒ Object
- #credentials_path ⇒ Object
-
#make_headers ⇒ Object
Make Auth Headers Strings for Amazon Product Advertising API 5.0 reference: webservices.amazon.com/paapi5/documentation/without-sdk.html webservices.amazon.com/paapi5/documentation/sending-request.html#signing important! webservices.amazon.co.jp/paapi5/scratchpad/.
-
#make_uri ⇒ Object
URI文字列の生成 create_search_itemsを呼び出していないと機能しない.
- #prepare_canonical_request(http_method_name, path, payload) ⇒ Object
- #prepare_string_to_sign(canonical_url) ⇒ Object
Instance Attribute Details
#access_key ⇒ Object (readonly)
Returns the value of attribute access_key.
10 11 12 |
# File 'lib/aws_v4_auth.rb', line 10 def access_key @access_key end |
#current_date ⇒ Object (readonly)
Returns the value of attribute current_date.
10 11 12 |
# File 'lib/aws_v4_auth.rb', line 10 def current_date @current_date end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
10 11 12 |
# File 'lib/aws_v4_auth.rb', line 10 def headers @headers end |
#http_method_name ⇒ Object (readonly)
Returns the value of attribute http_method_name.
10 11 12 |
# File 'lib/aws_v4_auth.rb', line 10 def http_method_name @http_method_name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
10 11 12 |
# File 'lib/aws_v4_auth.rb', line 10 def path @path end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
10 11 12 |
# File 'lib/aws_v4_auth.rb', line 10 def payload @payload end |
#region ⇒ Object (readonly)
Returns the value of attribute region.
10 11 12 |
# File 'lib/aws_v4_auth.rb', line 10 def region @region end |
#secret_key ⇒ Object (readonly)
Returns the value of attribute secret_key.
10 11 12 |
# File 'lib/aws_v4_auth.rb', line 10 def secret_key @secret_key end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
10 11 12 |
# File 'lib/aws_v4_auth.rb', line 10 def service @service end |
#signed_headers ⇒ Object (readonly)
Returns the value of attribute signed_headers.
10 11 12 |
# File 'lib/aws_v4_auth.rb', line 10 def signed_headers @signed_headers end |
#xamz_date ⇒ Object (readonly)
Returns the value of attribute xamz_date.
10 11 12 |
# File 'lib/aws_v4_auth.rb', line 10 def xamz_date @xamz_date end |
Class Method Details
.create_search_items(access_key, secret_key, partner_tag, keywords:, item_page: 1, min_price: 1, max_price: 100000, min_review_rating: 1, host: "webservices.amazon.co.jp", region: "us-west-2", market_place: "www.amazon.co.jp") ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/aws_v4_auth.rb', line 121 def create_search_items(access_key, secret_key, partner_tag, keywords:, item_page: 1, min_price: 1, max_price: 100000, min_review_rating: 1, host: "webservices.amazon.co.jp", region: "us-west-2", market_place: "www.amazon.co.jp") service = 'ProductAdvertisingAPI' http_method_name = 'POST' api_path = '/paapi5/searchitems' headers = {} headers['Content-Encoding'] = 'amz-1.0' headers['Host'] = host headers['X-Amz-Target'] = 'com.amazon.paapi5.v1.ProductAdvertisingAPIv1.SearchItems' request_payload = <<~EOS { "Keywords": "#{keywords}", "Resources": [ "BrowseNodeInfo.WebsiteSalesRank", "CustomerReviews.Count", "CustomerReviews.StarRating", "Images.Primary.Small", "Images.Primary.Medium", "ItemInfo.ByLineInfo", "ItemInfo.ContentInfo", "ItemInfo.ContentRating", "ItemInfo.Classifications", "ItemInfo.ManufactureInfo", "ItemInfo.ProductInfo", "ItemInfo.TechnicalInfo", "ItemInfo.Title", "ItemInfo.TradeInInfo", "Offers.Listings.Condition", "Offers.Listings.Condition.ConditionNote", "Offers.Listings.Condition.SubCondition", "Offers.Listings.DeliveryInfo.IsAmazonFulfilled", "Offers.Listings.DeliveryInfo.IsFreeShippingEligible", "Offers.Listings.DeliveryInfo.IsPrimeEligible", "Offers.Listings.DeliveryInfo.ShippingCharges", "Offers.Listings.IsBuyBoxWinner", "Offers.Listings.LoyaltyPoints.Points", "Offers.Listings.MerchantInfo", "Offers.Listings.Price", "Offers.Listings.ProgramEligibility.IsPrimeExclusive", "Offers.Listings.ProgramEligibility.IsPrimePantry", "Offers.Listings.Promotions", "Offers.Listings.SavingBasis", "Offers.Summaries.HighestPrice", "Offers.Summaries.LowestPrice", "Offers.Summaries.OfferCount", "ParentASIN", "SearchRefinements" ], "SearchIndex": "All", "PartnerTag": "#{partner_tag}", "PartnerType": "Associates", "Marketplace": "#{market_place}", "Operation": "SearchItems", "ItemPage": #{item_page}, "MinReviewsRating": #{}, "MinPrice": #{min_price}, "MaxPrice": #{max_price}, "CurrencyOfPreference":"JPY", "Condition":"New", "DeliveryFlags":["FulfilledByAmazon"] } EOS Auth.new(access_key, secret_key, api_path, region, service, http_method_name, headers, request_payload) end |
.hmac_sha_256(key, base_string) ⇒ Object
199 200 201 |
# File 'lib/aws_v4_auth.rb', line 199 def hmac_sha_256(key, base_string) OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), key, base_string) end |
.utc_timestamp(time) ⇒ Object
203 204 205 206 |
# File 'lib/aws_v4_auth.rb', line 203 def (time) now = time.utc return now.strftime("%Y%m%dT%H%M%SZ"), now.strftime("%Y%m%d") end |
Instance Method Details
#build_authorization_string(signature) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/aws_v4_auth.rb', line 81 def (signature) = [] << "Credential=#{credentials_path}" << "SignedHeaders=#{@signed_headers.join(";")}" << "Signature=#{signature}" "#{HMAC_ALGORITHM} #{.join(", ")}" end |
#calculate_signature(string_to_sign) ⇒ Object
77 78 79 |
# File 'lib/aws_v4_auth.rb', line 77 def calculate_signature(string_to_sign) OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), signature_key(), string_to_sign) end |
#credentials_path ⇒ Object
89 90 91 92 |
# File 'lib/aws_v4_auth.rb', line 89 def credentials_path credentials = [@access_key, @current_date, region, service, AWS4_REQUEST] credentials.join("/") end |
#make_headers ⇒ Object
Make Auth Headers Strings for Amazon Product Advertising API 5.0 reference: webservices.amazon.com/paapi5/documentation/without-sdk.html
https://webservices.amazon.com/paapi5/documentation/sending-request.html#signing
important! webservices.amazon.co.jp/paapi5/scratchpad/
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/aws_v4_auth.rb', line 31 def make_headers @headers['X-Amz-Date'] = @xamz_date @headers['X-Amz-Content-Sha256'] = OpenSSL::Digest::SHA256.hexdigest(@payload) canonical_url = prepare_canonical_request(@http_method_name, @path, @headers['X-Amz-Content-Sha256']) string_to_sign = prepare_string_to_sign(canonical_url) signature = calculate_signature(string_to_sign) @headers['Authorization'] = (signature) Hash[@headers] end |
#make_uri ⇒ Object
URI文字列の生成 create_search_itemsを呼び出していないと機能しない
44 45 46 |
# File 'lib/aws_v4_auth.rb', line 44 def make_uri "https://#{@headers['Host']}#{@path}" end |
#prepare_canonical_request(http_method_name, path, payload) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/aws_v4_auth.rb', line 48 def prepare_canonical_request(http_method_name, path, payload) canonical_url = [] uri = URI.parse(path.to_s) canonical_url << http_method_name canonical_url << "#{uri.path}\n" @signed_headers = [] headers = @headers.sort_by(&:first) canonical_headers = [] headers.each do |key, value| @signed_headers << key.downcase canonical_headers << "#{key.downcase}:#{value}" end canonical_url << "#{canonical_headers.join("\n")}\n" canonical_url << @signed_headers.join(";") canonical_url << payload canonical_url.join("\n") end |
#prepare_string_to_sign(canonical_url) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/aws_v4_auth.rb', line 68 def prepare_string_to_sign(canonical_url) string_to_sign = [] string_to_sign << HMAC_ALGORITHM string_to_sign << @xamz_date string_to_sign << "#{@current_date}/#{@region}/#{@service}/#{AWS4_REQUEST}" string_to_sign << OpenSSL::Digest::SHA256.hexdigest(canonical_url) string_to_sign.join("\n") end |