Jason Kilhoffer
Jason Kilhoffer

Reputation: 13

Amazon Affiliate API using C#.NET and Nager Amazon Product Advertising

I am at the initial stage of getting my project to build with the Nuget package.

I have installed the Nuget package: https://github.com/nager/Nager.AmazonProductAdvertising

I have started up a 4.8 .NET Framework C# console application solution and it has one C# project with the following code:

using System;
using System.Threading.Tasks;
using Nager.AmazonProductAdvertising;
using Nager.AmazonProductAdvertising.Model;

namespace AmazonProductAdvertisingExample
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var accessKey = "yourAccessKey";
            var secretKey = "yourSecretKey";
            var associateTag = "yourAssociateTag";

            var authentication = new AmazonAuthentication(accessKey, secretKey);
            var client = new AmazonProductAdvertisingClient(authentication, AmazonEndpoint.US, associateTag);

            await SearchProductsAsync(client, "programming books");
        }

        private static async Task SearchProductsAsync(AmazonProductAdvertisingClient client, string keywords)
        {
            var searchRequest = new SearchRequest
            {
                Keywords = keywords,
                Resources = new[] {
                    "ItemInfo.Title",
                    "ItemInfo.ByLineInfo",
                    "Offers.Listings.Price"
                }
            };

            var response = await client.SearchItemsAsync(searchRequest);
            if (response.Success)
            {
                foreach (var item in response.SearchResult.Items)
                {
                    Console.WriteLine($"{item.ASIN} - {item.ItemInfo.Title.DisplayValue}");
                }
            }
            else
            {
                Console.WriteLine($"Failed to search items: {response.Error.Message}");
            }
        }
    }
}

I have four errors and cannot compile:

  1. Severity Code Description Project File Line Suppression State Error CS0246 The type or namespace name 'AmazonProductAdvertisingClient' could not be found (are you missing a using directive or an assembly reference?)

  2. Severity Code Description Project File Line Suppression State Error CS1729 'AmazonAuthentication' does not contain a constructor that takes 2 arguments

  3. Severity Code Description Project File Line Suppression State Error CS0246 The type or namespace name 'AmazonProductAdvertisingClient' could not be found (are you missing a using directive or an assembly reference?)

  4. Severity Code Description Project File Line Suppression State Error CS0246 The type or namespace name 'SearchRequest' could not be found (are you missing a using directive or an assembly reference?)

Upvotes: 1

Views: 237

Answers (1)

Jason Kilhoffer
Jason Kilhoffer

Reputation: 13

Ok to get this to work I did the following:

  1. Downloaded and installed .NET Core SDK 3.1
  2. Uninstalled just the Nager.AmazonProductAdvertising NuGet package.
  3. Downloaded the Nager.AmazonProductAdvertising SRC code and added the Nager.AmazonProductAdvertising project to my solution.
  4. Referenced the Nager.AmazonProductAdvertising project to my own.
  5. Changed response.Success to response.Successful
  6. Changed response.Error.Message to response.ErrorMessage

I am happy it's working but have no understanding of why I had to do it this way. If anyone wants to pipe in on that.

Upvotes: 0

Related Questions