API Authorization using MVC C# . Base64 Authorization Basic encryption

I seem to be having ALOT of trouble getting the API to grant me access after attempting to use Base64 Basic Authorization with my API key. Has anyone here built their site specifically on ASP.NET MVC C#. Were you able to return the desired data for an order after implementing the Base64 Basic Authorization with your API key ?

Please read below so you see what I have done so far and where I am getting stuck:

I have a controller called PurchaseController.cs and an action result inside of that controller called public ActionResult Thankyou(). When I complete a transaction, it takes the user to my custom thank you page in my PurchaseController. Once the user has landed on that page I would like to display information about the order such as the invoiceNumber, Address, Name, Email, Total etc… I understand that in order to grab that info and show it to the customer I have to add the logic for the API to grant access via the secret api key.

Currently, when my code gets to the TRY CATCH its supposed to already have access since I provided the api key in the format it wanted, but I see that it just returns a 500 error. When debugging and hovering the exception I get: “The remote server returned an error: (500) Internal Server Error.”

(Note: I am adding the token of a test order to the api call statically for now until I get this resolved)

public ActionResult Thankyou(string slug, string Token, string email)
        {
            slug = "thank-you";

            //https://docs.snipcart.com/v3/api-reference/orders
            //https://docs.snipcart.com/v3/api-reference/authentication

            //Test Static URL with static token number for testing purposes 
            var url = "https://app.snipcart.com/api/orders/a80210a1-f2f4-0021-9051-bea701452012";

            var httpRequest = (HttpWebRequest)WebRequest.Create(url);
            httpRequest.Accept = "application/json";        
            httpRequest.AllowAutoRedirect = true;
            httpRequest.ContentType = "application/json";
            httpRequest.Method = "GET";
                       
            var basicAuthHeader = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("API_KEY_STRING"));
            
            httpRequest.Headers.Add("Authorization", "Basic " + basicAuthHeader + ":"); //You HAVE to put a space after the word "Basic " otherwise it will fail.
                        


            //This keeps throwing this 500 error so I read that I should use a Try Catch instead.....
            //var httpResponse = (HttpWebResponse)httpRequest.GetResponse();  
            HttpWebResponse httpResponse;
            try
            {
                httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            }
            catch (WebException ex)
            {
                httpResponse = (HttpWebResponse)ex.Response; //The remote server returned an error: (500) Internal Server Error.
            }

            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
            }

            Console.WriteLine(httpResponse.StatusCode);


            return View();

            // https://docs.snipcart.com/v3/api-reference/orders
            //These are the items that I would like to load on the thank you page for the customer to see.
            //invoiceNumber
            //productId
            //status
            //placedBy
            //email
            //paymentMethod
            


        }

I have tried to write the code as the documentation says where I should add the single colon at the end of the encrypted api key and have also written the code to call for Authorization Basic and convert it to Base64… I am really stuck here… Any help on the code from anyone who can do this is C# ?

(Note all of the data here is TEST data as I have not switched to LIVE orders yet. So the api key that I am using is my public test api key.)

Hi @EdRod, thanks for reaching out.

You need to add the : to the key before converting to base64.

var basicAuthHeader = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("YOUR_API_KEY"  + ":"));
            
httpRequest.Headers.Add("Authorization", "Basic " + basicAuthHeader);

Can you test this out?

Thanks.

Hey @nelitow thanks for that suggestion. Yes I have tried that and I get this 401 error when I try to add the colon before the conversion-

"Message = “The remote server returned an error: (401) Unauthorized.”

I have also tried to remove the space that comes after "Basic " and still get that 401 Unauthorized.

I have tried all of these scenarios below just now

//Colon before conversion with space after Basic
var basicAuthHeader = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("YOUR_API_KEY"  + ":"));            
httpRequest.Headers.Add("Authorization", "Basic " + basicAuthHeader);

//Colon before conversion with no space after Basic
var basicAuthHeader = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("YOUR_API_KEY"  + ":"));            
httpRequest.Headers.Add("Authorization", "Basic" + basicAuthHeader);

//Space after Basic and no colon before conversion
var basicAuthHeader = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("YOUR_API_KEY"));            
httpRequest.Headers.Add("Authorization", "Basic " + basicAuthHeader + ":");

//No space after Basic
var basicAuthHeader = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("YOUR_API_KEY"));            
httpRequest.Headers.Add("Authorization", "Basic" + basicAuthHeader + ":");

Hey @EdRod,

Since the default encoding in .NET is UTF-8, I think the problem here is that you use ASCII as the source encoding rather than UTF-8.

Hope this helps!

Cheers,

Thanks for the replies guys, I was able to get this resolved with a follow up email from support earlier today. It ended up being 2 things that I might have been doing wrong.

  1. As you stated @nelitow yes I have to add the colon to the key before it gets converted to base64.

  2. I was using the Public API key in those calls and not the Secret Api Key. I figured since these were test transactions I was supposed to use the test public key. Turns out I should have been using the Secret Key instead even for test transactions. I was really confused at that part which is why I guess I kept trying to make it work with the incorrect key.

1 Like

Thanks for the return @EdRod

Maybe we can rewrite the docs to make this clearer.

We appreciate the feedback.

Thanks!