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.)