Unsupported private key format amazon pay

Viewed 188

I have been trying to integrate amazon pay with my mvc application using amazon sdk for .net. i followed the steps mention here. I am able to create the client using the private key and public key created in the seller account.

using Amazon.Pay.API.Types;
using Amazon.Pay.API.WebStore;

public class Sample
{
    public WebStoreClient InitiateClient()
    {
        // set up config
        var payConfiguration = new ApiConfiguration
        (
            region: Region.Europe,
            publicKeyId: "MY_PUBLIC_KEY_ID", // LIVE-XXXXX or SANDBOX-XXXXX
            privateKey: "PATH_OR_CONTENT_OF_MY_PRIVATE_KEY"
        );

        // init API client
        var client = new WebStoreClient(payConfiguration);

        return client;
    }
}

using the client when i am trying to create the signature i am getting exception as unsupported private key format

using Amazon.Pay.API.WebStore.CheckoutSession;
using Amazon.Pay.API.WebStore;

public class Sample : PageModel
{
    // ..

    public string Signature { get; private set; }

    public string Payload { get; private set; }

    public void OnGet()
    {
        // prepare the request
        var request = new CreateCheckoutSessionRequest
        (
            checkoutReviewReturnUrl: "https://example.com/review.html",
            storeId: "amzn1.application-oa2-client.000000000000000000000000000000000"
        );

        // generate the button signature
        var signature = client.GenerateButtonSignature(request);

        // generate the signature and payload string that is passed back to the frontend
        Signature = client.GenerateButtonSignature(request);
        Payload = request.ToJson();
    }
}

stack trace of the exception

   at Amazon.Pay.API.SignatureHelper.GenerateSignature(String stringToSign)
   at Amazon.Pay.API.WebStore.WebStoreClient.GenerateButtonSignature(String jsonString)
   at Amazon.Pay.API.WebStore.WebStoreClient.GenerateButtonSignature(CreateCheckoutSessionRequest request)
1 Answers

There are two ways to supply the private key: 1/full file path of the .pem file or 2/string contents of the .pem

You didn't mention which option you chose, so I'll give a quick overview of each.

  1. full path to .pem file. I'd recommend setting a debug breakpoint prior to the init of the WebStoreClient to ensure that the path you supplied is valid and the file contents actually loaded. This is the most common cause for the error - invalid file path, resulting in no private key being loaded.

    var payConfiguration = new ApiConfiguration
        (
            region: Region.Europe,
            publicKeyId: "YOUR_PUBLIC_KEY_ID", 
            privateKey: "C:\\temp\\ap.pem"
        );
    
  2. string content of .pem file. The most common cause for error here is a copy/paste mistake where special characters may be getting added inadvertently.

    var payConfiguration = new ApiConfiguration
       (
           region: Region.Europe,
           publicKeyId: "YOUR_PUBLIC_KEY_ID", 
           privateKey: "-----BEGIN PRIVATE KEY-----\nXXXXX\n-----END PRIVATE KEY-----"
       );
    

I would always caution against hard coding either the string or path of the private key, and encourage loading these credentials from a secure key vault whenever possible.

Related