Search code examples
asp.netrestbasic-authenticationhammock

Using Hammock to Call Recurly REST API with BasicAuthCredentials


I'm trying to call the Recurly REST API using the Hammock C# library for .NET. The API calls require an Authorization header on the HttpRequest, and the authentication scheme is Basic authentication with the Base64 encoded API key in the username portion of the header. I thought that I could create a new Hammock BasicAuthCredentials object with the encoded key in the Username property of the object, then assign the object to the Credentials property of either the RestClient or RestRequest objects. However, this does not seem to generate an Authorization header on the outbound HttpRequest.

If I add the Authorization header manually using the AddHeader method on one of those objects, the API call succeeds. If I use the Credentials property with the BasicAuthCredentials object, I get an Access Denied error from Recurly.

This seems pretty basic, so I know I'm doing something wrong. So, in Hammock, is the Credentials property on either the RestClient or RestRequest object supposed to create an Authorization header on the Http request?

Thanks for any help from a super Hammock user!

The code that fails:

class Program
{
    public static void Main(string[] args)
    {

        string encodedAPIKey = Convert.ToBase64String(Encoding.UTF8.GetBytes("APIKeyHere"));
        BasicAuthCredentials credentials = new BasicAuthCredentials
        {
            Username = encodedAPIKey
        };

        RestClient client = new RestClient
        {
            Authority = "https://api.recurly.com",
            VersionPath = "v2"
        };

        client.AddHeader("Accept", "application/xml");

        RestRequest request = new RestRequest
        {
            Credentials = credentials,
            Path = "plans"

        };

        RestResponse response = client.Request(request);

        Console.WriteLine(response.Content);
        Console.ReadLine();


    }
}

The code that succeeds:

class Program
{
    public static void Main(string[] args)
    {

        string encodedAPIKey = Convert.ToBase64String(Encoding.UTF8.GetBytes("APIKeyHere"));

        RestClient client = new RestClient
        {
            Authority = "https://api.recurly.com",
            VersionPath = "v2"
        };

        client.AddHeader("Accept", "application/xml");
        client.AddHeader("Authorization", "Basic " + encodedAPIKey);

        RestRequest request = new RestRequest
        {
            Path = "plans"

        };

        RestResponse response = client.Request(request);

        Console.WriteLine(response.Content);
        Console.ReadLine();


    }
}

Solution

  • After getting no answers to my question, I did a search for alternative Rest libraries for .NET and found RestSharp. I was able to get it working with Recurly using its built-in Basic Authorization implementation on the first try, so I will be implementing using RestSharp. The code looks very similar, so the migration should be an easy one.