mardi 2 juin 2015

Amazon MWS C# class to do UPC lookup

I am very new to APIs

I have managed to get the Amazon MWS product api sample to work but I cant seem to integrate it into my existing app as a class - What I want to be able to do is pass a UPC into the class and it pass back the item title. Here are the pieces of the sample that i modified

    public class MarketplaceWebServiceProductsSample {

    public static void Main(string[] args)
    {
        // TODO: Set the below configuration variables before attempting to run

        // Developer AWS access key
        string accessKey = "AKEY";

        // Developer AWS secret key
        string secretKey = "SKEY";

        // The client application name
        string appName = "CSharpSampleCode";

        // The client application version
        string appVersion = "1.0";

        // The endpoint for region service and version (see developer guide)
        // ex: http://ift.tt/1oeEaX7
        string serviceURL = "http://ift.tt/1oeEaX7";

        // Create a configuration object
        MarketplaceWebServiceProductsConfig config = new MarketplaceWebServiceProductsConfig();
        config.ServiceURL = serviceURL;
        // Set other client connection configurations here if needed
        // Create the client itself
        MarketplaceWebServiceProducts client = new MarketplaceWebServiceProductsClient(appName, appVersion, accessKey, secretKey, config);

        MarketplaceWebServiceProductsSample sample = new MarketplaceWebServiceProductsSample(client);

        // Uncomment the operation you'd like to test here
        // TODO: Modify the request created in the Invoke method to be valid

        try 
        {
            IMWSResponse response = null;
             response = sample.InvokeGetMatchingProductForId();
            Console.WriteLine("Response:");
            ResponseHeaderMetadata rhmd = response.ResponseHeaderMetadata;
            // We recommend logging the request id and timestamp of every call.
            Console.WriteLine("RequestId: " + rhmd.RequestId);
            Console.WriteLine("Timestamp: " + rhmd.Timestamp);
            string strXML = response.ToXML().ToString();


            Regex r = new Regex("<ns2:Title>(.*)</ns2:Title>");
            Match m = r.Match(strXML);
            Console.WriteLine(m.Groups[1].ToString());

        }
        catch (MarketplaceWebServiceProductsException ex)
        {
            // Exception properties are important for diagnostics.
            ResponseHeaderMetadata rhmd = ex.ResponseHeaderMetadata;
            Console.WriteLine("Service Exception:");
            if(rhmd != null)
            {
                Console.WriteLine("RequestId: " + rhmd.RequestId);
                Console.WriteLine("Timestamp: " + rhmd.Timestamp);
            }
            Console.WriteLine("Message: " + ex.Message);
            Console.WriteLine("StatusCode: " + ex.StatusCode);
            Console.WriteLine("ErrorCode: " + ex.ErrorCode);
            Console.WriteLine("ErrorType: " + ex.ErrorType);
            throw ex;
        }
    }

public GetMatchingProductForIdResponse InvokeGetMatchingProductForId()
    {
        // Create a request.
        GetMatchingProductForIdRequest request = new GetMatchingProductForIdRequest();
        string sellerId = "SellerID";
        request.SellerId = sellerId;
        //string mwsAuthToken = "example";
        //request.MWSAuthToken = mwsAuthToken;
        string marketplaceId = "Marketplace";
        request.MarketplaceId = marketplaceId;
        string idType = "UPC";
        request.IdType = idType;
        IdListType idList = new IdListType();
        idList.Id.Add("I want to pass a UPC Here");
        request.IdList = idList;
        return this.client.GetMatchingProductForId(request); //and have it return the title to my app - the regex above strips everything but the title from the response 
    }

amazons sample uses many classes and I am getting lost in them - I just want to pass a UPC in and get the item Title back - its a WinForm app

There is a config file that was with the sample but i made no changes to it.




Aucun commentaire:

Enregistrer un commentaire