Mike Cave Mike Cave - 1 month ago 16
C# Question

Purge LeaseWeb CDN using LeaseWeb CDN API in POSTMAN

I am having difficulty sending a purge request to LeaseWeb CDN API. The difficulty is that the LeaswebExamples are in PHP/Python and don't have any info on how to do it in Post man.
The curl example is nearest:


curl -X POST -d '{"urls":["/path/to/file.jpg"]}' "https://api.leasewebcdn.com/content/purge/1234567890/123/1440593540/4a69f766bc48b1ed3d025339313196c388de8da5"


but I always get "forbidden" back from Postman.

I'm creating an sha1 hash using secretKey + timeStamp + authenticationURL; in c#:

var authenticationURL = "https://api.leasewebcdn/content/purge";
var sha1 = new SHA1Managed();
var timeStamp = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;

var sha1Input = secretKey + timeStamp + authenticationURL;

var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(sha1Input));
var sb = new StringBuilder(hash.Length);

foreach (byte b in hash)
{
// can be "x2" if you want lowercase
sb.Append(b.ToString("X2"));
}


Which I then use in Postman to send the following:

https://api.leasewebcdn.com/content/purge/1405123458/8888/1476699042/D94CF7DDF02CC6D11C7A0B0221F5DE40C11155E01

where
1405123458 = customer num

8888 = zone id

D94CF7DDF02CC6D11C7A0B0221F5DE40C11155E01 = hashed signature form code

contenttype = application/json
body = {"urls": ["*"]} <- will purge all files in cdn bucket

Everytime i get forbidden back and I cant work out what is wrong as all the parameters are correct, as far as I can see.

The Leaseweb CDN API docs are in the following link, but they are all in PHP /Python and I hav a feeling my hash method may not be right?

http://developer.leaseweb.com/cdn-docs/?php#purge-file(s)-from-a-zone

Has anyone used Leaseweb CDN api via C# before? Any help or pointers would be gratefully received.

Answer

Okay I managed to solve this. The code was actually all good except for the Hash signature. For LeaseWeb the Hash Signature must always be in lower case. This can be achieved by tweaking the above code by 1 (one!!) character:

    var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(sha1Input));
    var sb = new StringBuilder(hash.Length);

    foreach (byte b in hash)
    {
        // this must be lowercase x2 (not X2) to produce a lowercase hash signature.
        sb.Append(b.ToString("x2"));
    }

I was surprised this wasn't specified in the LeaseWeb CDN Documentation as it caused me to waste a lot of time figuring it out....

Comments