Authenticating With the ShipStation API

The sample project provides the API credentials to a ShipStation endpoint as an Authorization header followed by the encoded string. The first task is to store the API Key and API Secret in the AuctionWorx web.config file.

  1. In your AuctionWorx project, open the web.config file.
  2. Locate the API Key and API Secret for your ShipStation Account and insert them in the <appSettings></appSettings> element as follows:
    <add key="ShipStationAPIAddress" value="https://ssapi.shipstation.com/" />
    <add key="ShipStationAPIKey" value="22188fb9e89e44e88ff72cf91874587d" />
    <add key="ShipStationAPISecret" value="4b83f761d27b4d16b0bf1ebe905325a1" />

In the example project, the authentication routine is part of the ShipStationClient class (ShipStationClient.cs). Because the authentication must be sent with every access, the credentials are fetched from the web.config file as part of the variable declarations:

        public string AuthorizationString = string.Empty;
        private Uri ShipStationAPIAddress = new Uri
            (ConfigurationManager.AppSettings["ShipStationAPIAddress"]);
        private string ShipStationAPIKey = ConfigurationManager.
            AppSettings["ShipStationAPIKey"];
        private string ShipStationAPISecret = ConfigurationManager.
            AppSettings["ShipStationAPISecret"];

The AddBasicHeader() method assembles the API Key and API Secret into an encoded Authorization header. It adds the new header along with another that alerts the ShipStation API that a JSON-formatted return string is acceptable for the response.

private void AddBasicHeader(HttpClient client)
 {
    string AuthorizationMethod = "Basic";
    AuthorizationString = AuthorizationMethod.Trim() + " "
                        + Convert.ToBase64String(
                            Encoding.UTF8.GetBytes(ShipStationAPIKey
                        + ":" + ShipStationAPISecret));
    client.DefaultRequestHeaders.Clear();
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add
                (new MediaTypeWithQualityHeaderValue("application/json"));
    client.DefaultRequestHeaders.Add
        ("Authorization", AuthorizationString);
 }

The preceding AddBasicHeader() method is called after creating the ShipStation Order object.

Copyright © 2002-2022. RainWorx Software. All rights reserved.