How to integrate Amazon Product API in iOS

Posted By : Nitin Bhatt | 10-Apr-2017

What is AWS Product API?

Through AWS product API we can access data used by Amzaon including the items for sales, seller reviews as well as most of the functionality we see on Amazon.com.

 

For more info about AWS product API please go through this link

http://docs.aws.amazon.com/AWSECommerceService/latest/DG/Welcome.html

 

For amazon product API integaretion on iOS first install all these libraies :- 


 

Write this code inside ViewController.swift file 


 

Create a new file GenerateAWSSignature.swift and write this code inside this file

 import Foundation
import AWSCore
import Alamofire

class AWSSignature {
    
    func signedParametersForParameters(_ parameters: [String: String]) -> [String:AnyObject]{
        let secretKey = getAmazonAccountCredentials().2
        let sortedKeys = Array(parameters.keys).sorted(by: <)
        var components: [(String, String)] = []
        
        for  key in sortedKeys {
            components += URLEncoding.queryString.queryComponents(fromKey: key, value: parameters[key]!)
        }
        
        let query = (components.map { "\($0)=\($1)" } as [String]).joined(separator: "&")
        let stringToSign = "GET\nwebservices.amazon.in\n/onca/xml\n\(query)"
        let dataToSign = stringToSign.data(using: String.Encoding.utf8)
        
        let signature = AWSSignatureSignerUtility.hmacSign(dataToSign, withKey: secretKey, usingAlgorithm: UInt32(kCCHmacAlgSHA256))!
        
        var params : [String:AnyObject] = parameters as [String : AnyObject]
        
        params["Signature"] = signature as AnyObject?
        
        
        
        return params
        
    }
    
    
    func itemSearchUsingBrowseId(_ page:String,browseNodeId:String,serachIndex:String,sorting:String,maximumPrice:String,minimumPrice:String) -> [String:AnyObject]{
        let accessKey = getAmazonAccountCredentials().0
        let associateTag = getAmazonAccountCredentials().1
        let timestamp = getTimestamp()
        var keyParams = ["Service": "AWSECommerceService", "Operation": "ItemSearch","BrowseNode": browseNodeId,"ItemPage": page,"Timestamp": timestamp.string(from: Date()),"AWSAccessKeyId": accessKey, "AssociateTag": associateTag,"ResponseGroup":"OfferSummary,Images,Medium,VariationSummary","SearchIndex":serachIndex,"Availability":"Available","Sort": sorting]
        
        if (maximumPrice.isEmpty == false) && (minimumPrice.isEmpty == false){
            keyParams["MaximumPrice"] = "\(maximumPrice)00"
            keyParams["MinimumPrice"] = "\(minimumPrice)00"
        }
        print("response parmas")
        print(keyParams)
        
        let globalSearchParams = signedParametersForParameters(keyParams)
        return globalSearchParams
    }
    
    
    func itemSearch(_ page:String,searchKeyword:String,searchIndex:String,maximumPrice:String,minimumPrice:String,sorting:String) -> [String:AnyObject]{
        let accessKey = getAmazonAccountCredentials().0
        let associateTag = getAmazonAccountCredentials().1
        let timestamp = getTimestamp()
        var keyParams = ["Service": "AWSECommerceService", "Operation": "ItemSearch","Keywords": searchKeyword,"ItemPage": page,"Timestamp": timestamp.string(from: Date()),"AWSAccessKeyId": accessKey, "AssociateTag": associateTag,"ResponseGroup":"OfferSummary,Images,Medium,VariationSummary","SearchIndex":searchIndex,"Availability":"Available"]
        
        if (searchIndex.lowercased() != "all"){
            keyParams["Sort"] = sorting
        }
        
        if (maximumPrice.isEmpty == false) && (minimumPrice.isEmpty == false){
            keyParams["MaximumPrice"] = "\(maximumPrice)00"
            keyParams["MinimumPrice"] = "\(minimumPrice)00"
        }
        
        print("response parmas")
        print(keyParams)
        
        let globalSearchParams = signedParametersForParameters(keyParams)
        return globalSearchParams
    }
    
    func browseNodeLookUp(_ browseNodeId:String)-> [String:AnyObject]{
        let accessKey = getAmazonAccountCredentials().0
        let associateTag = getAmazonAccountCredentials().1
        let timestamp = getTimestamp()
        let keyParams = ["Service": "AWSECommerceService", "Operation": "BrowseNodeLookup", "BrowseNodeId": browseNodeId, "ResponseGroup": "BrowseNodeInfo","AWSAccessKeyId": accessKey, "AssociateTag": associateTag, "Timestamp": timestamp.string(from: Date())]
        let globalSearchParams = signedParametersForParameters(keyParams)
        return globalSearchParams
    }
    
    
    func itemLookUp(_ asin:String) -> [String:AnyObject]{
        let accessKey = getAmazonAccountCredentials().0
        let associateTag = getAmazonAccountCredentials().1
        let timestamp = getTimestamp()
        let keyParams = ["Service": "AWSECommerceService", "Operation": "ItemLookup","ItemId": asin ,"Timestamp": timestamp.string(from: Date()),"AWSAccessKeyId": accessKey, "AssociateTag": associateTag,"ResponseGroup":"Offers,Images,Medium,VariationSummary,VariationMatrix","idType": "ASIN"]
        
        
        print("response parmas")
        print(keyParams)
        
        let globalSearchParams = signedParametersForParameters(keyParams)
        return globalSearchParams
    }
    
    
    
    func getAmazonAccountCredentials()->(String,String,String){
        let AWSAccessKey = "-----"
        let AssociateTag = "-----"
        let AWSSecretKey = "----"
        
        return (AWSAccessKey,AssociateTag,AWSSecretKey)
        
    }
    
    func getTimestamp() -> DateFormatter{
        var timestampFormatter = DateFormatter()
        timestampFormatter = DateFormatter()
        timestampFormatter.dateFormat = AWSDateISO8601DateFormat3
        timestampFormatter.timeZone = TimeZone(identifier: "GMT")
        timestampFormatter.locale = Locale(identifier: "en_US_POSIX")
        return timestampFormatter
    }
}

 

Create an AWS account using this link :-

https://portal.aws.amazon.com/billing/signup?redirect_url=https%3A%2F%2Faws.amazon.com%2Fregistration-confirmation

 

After that get your AWSAccessKey, AssociateTag, AWSSecretKey from AWS account and add inside this function

 func getAmazonAccountCredentials()->(String,String,String){
    let AWSAccessKey = "-----"
    let AssociateTag = "-----"
    let AWSSecretKey = "----"
    
    return (AWSAccessKey,AssociateTag,AWSSecretKey)
    
}
 

 

 

About Author

Author Image
Nitin Bhatt

Nitin is an Assistant Project Manager specializing in iOS Application Development. He is an avid reader.

Request for Proposal

Name is required

Comment is required

Sending message..