How to convert the affiliate scratchpad api code from java to C# and make it work?

0

Hi there,

Apologise if I have posted this in the wrong area, so I have a C# .net core application and all it needs to do at the moment is bring back one product, its condition, the image, price and afiliate link. I have used the affiliate api scratchpad to enter my credentials and send the request - its works, happy days!

However, the code is in Java and I don't know this, I have attempted to convert this to C# but to no avil, here is my attempt:

string requestPayload =
"{"
+ "\"Keywords\": \"yoshi\","
+ "\"Resources\": ["
+ "\"Images.Primary.Medium\","
+ "\"ItemInfo.Title\","
+ "\"Offers.Listings.Condition\","
+ "\"Offers.Listings.Price\""
+ "],"
+ "\"ItemCount\": 1,"
+ "\"PartnerTag\": \"###\","
+ "\"PartnerType\": \"Associates\","
+ "\"Marketplace\": \"www.amazon.co.uk\""
+ "}";

Dictionary<string, string> headers = new Dictionary<string, string>
{
    {"host", HOST},
    {"content-type", "application/json; charset=UTF-8"},
    {"x-amz-target", "com.amazon.paapi5.v1.ProductAdvertisingAPIv1.SearchItems"},
    {"content-encoding", "amz-1.0"}
};


var awsv4Auth = new AWSV4Auth.Builder(ACCESS_KEY, SECRET_KEY)
    .Path(URI_PATH)
    .Region(REGION)
    .Service("ProductAdvertisingAPI")
    .HttpMethodName("POST")
    .Headers(headers)
    .Payload(requestPayload)
    .Build();

using (var httpClient = new HttpClient())
{
    var uri = new Uri($"https://{HOST}{URI_PATH}");
    var request = new HttpRequestMessage(HttpMethod.Post, uri);
    request.Content = new StringContent(requestPayload, Encoding.UTF8, "application/json");

    var header = awsv4Auth.GetHeaders();
    foreach (var (key, value) in header)
    {
        request.Headers.Add(key, value);
    }

    var response = await httpClient.SendAsync(request);
    var jsonResponse = await response.Content.ReadAsStringAsync();
    var statusCode = (int)response.StatusCode;

    Console.WriteLine(jsonResponse);

    if (statusCode == 200)
    {
        Console.WriteLine("Successfully received response from Product Advertising API.");
        Console.WriteLine(jsonResponse);
    }
    else
    {
        var json = JObject.Parse(jsonResponse);
        if (json.ContainsKey("Errors"))
        {
            var errorArray = json["Errors"] as JArray;
            foreach (var error in errorArray)
            {
                Console.WriteLine($"Error Code: {error["Code"]}, Message: {error["Message"]}");
            }
        }
        else
        {
            Console.WriteLine("Error Code: InternalFailure, Message: The request processing has failed because of an unknown error, exception or failure. Please retry again.");
        }
    }
}

The HOST is "webservices.amazon.co.uk", the URI_PATH is /paapi5/searchitems and REGION "eu-west-1". I have a couple of errors:

The type or namespace name 'AWSV4Auth' could not be found (are you missing a using directive or an assembly reference? Cannot infer the type of implicitly-typed deconstruction variable 'key'. Cannot infer the type of implicitly-typed deconstruction variable 'value'.

I guess these are related to this being C# and I'm attempting to reference a Java namespace AWSV4Auth

Similar issue here with the key and value awsv4Auth

Anyone know what I should be using instead of the AWSV4Auth namespace? Or have example code that works?

No Answers

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions