So here I'm going to show how to use Akamai Fast Purge with c#. For this, you must have Akamai control center access to get the API client details.
Note: You can see the secret key only once while creating New Client Token. You need to download it or save it in the separate notepad while the UI displaying it.
Now we got the required credentials, next we need to authenticate our API using those credentials with Edgegrid signer.
Note: Edgegrid signer is the nugget package, you can get it from the below link.
https://www.nuget.org/packages/Akamai.EdgeGrid.Auth/
You need to install this package in your visual studio project to authenticate your API.
So after installing the package in your project, you have to do the following things:
1. Using EdgeGridV1Signer - sign method, you need to sign your web request.
Example:
/// <summary>
/// Get Authorization header value after signing the request using Edgegrid signer
/// </summary>
/// <param name="propertyValues"></param>
/// <param name="hostUrlToValidate"></param>
/// <returns></returns>
private string GetEdgeGridSignerAuthenticationtoken(AkamaiProperties propertyValues, string hostUrlToValidate)
{
int maxBodySize = 8192;
string jsonString = propertyValues.JsonString;
string clientToken = propertyValues.clienttoken;
string accessToken = propertyValues.accesstoken;
string secretKey = propertyValues.secretkey;
string hostUrl = hostUrlToValidate;
//Create Web request
var credential = new ClientCredential(clientToken, accessToken, secretKey);
var uri = new Uri(hostUrl);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = Constants.AkamaiCacheHandler.POST_METHOD;
request.ContentType = Constants.AkamaiCacheHandler.CONTENT_TYPE_JSON;
//Convert input json string to stream to sign the request using EdgeGrid signer
Stream uploadStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
//signing using Akamai Edge grid
var signer = new EdgeGridV1Signer(null, maxBodySize);
signer.Sign(request, credential, uploadStream);
//Get Authorization header value which requires for API V3
string authValue = request.Headers.Get(Constants.AkamaiCacheHandler.AUTHORIZATION_HEADER);
return authValue;
}
Note:
After signing, you will get the new token in the header named "authorization". You must send this token along with the akamai web request.
Example authorization header value:
EG1-HMAC-SHA256 client_token=akab-mgxxxktj4flkcb7-qdtdsoxxxw;access_token=akab-gzxxxomj7tjglxxxw-gdtio4sxxxz;timestamp=20191108T23:55:43+0000;nonce=60fbffc9-c34a-c982-925d-84c44e6c378a;signature=KV2C8Ht6Y/dLejYPNTiL078PXmnHkeIa9mpmESWAWEA=
2. Make a web request with "authorization" header:
So now you got the required authorization token to access the akamai fast purge api so that you can make the web request call as below:
string result;
HttpWebRequest akamaiRequest = (HttpWebRequest)WebRequest.Create(actualHost);
akamaiRequest.Method = Constants.AkamaiCacheHandler.POST_METHOD;
akamaiRequest.ContentType = Constants.AkamaiCacheHandler.CONTENT_TYPE_JSON;
akamaiRequest.Headers.Add(Constants.AkamaiCacheHandler.AUTHORIZATION_HEADER, authValue);
//convert json string to byte array
byte[] byteArray = Encoding.UTF8.GetBytes(jsonString);
akamaiRequest.ContentLength = byteArray.Length;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
akamaiRequest.ServicePoint.Expect100Continue = false;
using (Stream requestStream = akamaiRequest.GetRequestStream())
{
requestStream.Write(byteArray, 0, byteArray.Length);
}
using (HttpWebResponse response = (HttpWebResponse)akamaiRequest.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}
}
Note: The highlighted auth value is nothing but the authorization header, the one we got from EdgeGridV1Signer - sign method .
If the web request is successful you will see the response like below:
{
"detail": "Request accepted",
"estimatedSeconds": 5,
"purgeId": "4dbff464-0283-11ea-a725-xxx6",
"supportId": "17PY1573257355655570-xxx7184",
"httpStatus": 201
}
There will be a status check page in Akamai v2 API, but thats not available in Akamai fast purge v3.
You need to create a new API client using the Akamai control center if you don't have one.
From the control center you need to get the below three things:
1. Access Token
2. Client Token
3. Secret Key
From the control center you need to get the below three things:
1. Access Token
2. Client Token
3. Secret Key
Note: You can see the secret key only once while creating New Client Token. You need to download it or save it in the separate notepad while the UI displaying it.
Now we got the required credentials, next we need to authenticate our API using those credentials with Edgegrid signer.
Note: Edgegrid signer is the nugget package, you can get it from the below link.
https://www.nuget.org/packages/Akamai.EdgeGrid.Auth/
You need to install this package in your visual studio project to authenticate your API.
So after installing the package in your project, you have to do the following things:
1. Using EdgeGridV1Signer - sign method, you need to sign your web request.
Example:
/// <summary>
/// Get Authorization header value after signing the request using Edgegrid signer
/// </summary>
/// <param name="propertyValues"></param>
/// <param name="hostUrlToValidate"></param>
/// <returns></returns>
private string GetEdgeGridSignerAuthenticationtoken(AkamaiProperties propertyValues, string hostUrlToValidate)
{
int maxBodySize = 8192;
string jsonString = propertyValues.JsonString;
string clientToken = propertyValues.clienttoken;
string accessToken = propertyValues.accesstoken;
string secretKey = propertyValues.secretkey;
string hostUrl = hostUrlToValidate;
//Create Web request
var credential = new ClientCredential(clientToken, accessToken, secretKey);
var uri = new Uri(hostUrl);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = Constants.AkamaiCacheHandler.POST_METHOD;
request.ContentType = Constants.AkamaiCacheHandler.CONTENT_TYPE_JSON;
//Convert input json string to stream to sign the request using EdgeGrid signer
Stream uploadStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
//signing using Akamai Edge grid
var signer = new EdgeGridV1Signer(null, maxBodySize);
signer.Sign(request, credential, uploadStream);
//Get Authorization header value which requires for API V3
string authValue = request.Headers.Get(Constants.AkamaiCacheHandler.AUTHORIZATION_HEADER);
return authValue;
}
Note:
After signing, you will get the new token in the header named "authorization". You must send this token along with the akamai web request.
Example authorization header value:
EG1-HMAC-SHA256 client_token=akab-mgxxxktj4flkcb7-qdtdsoxxxw;access_token=akab-gzxxxomj7tjglxxxw-gdtio4sxxxz;timestamp=20191108T23:55:43+0000;nonce=60fbffc9-c34a-c982-925d-84c44e6c378a;signature=KV2C8Ht6Y/dLejYPNTiL078PXmnHkeIa9mpmESWAWEA=
2. Make a web request with "authorization" header:
So now you got the required authorization token to access the akamai fast purge api so that you can make the web request call as below:
string result;
HttpWebRequest akamaiRequest = (HttpWebRequest)WebRequest.Create(actualHost);
akamaiRequest.Method = Constants.AkamaiCacheHandler.POST_METHOD;
akamaiRequest.ContentType = Constants.AkamaiCacheHandler.CONTENT_TYPE_JSON;
akamaiRequest.Headers.Add(Constants.AkamaiCacheHandler.AUTHORIZATION_HEADER, authValue);
//convert json string to byte array
byte[] byteArray = Encoding.UTF8.GetBytes(jsonString);
akamaiRequest.ContentLength = byteArray.Length;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
akamaiRequest.ServicePoint.Expect100Continue = false;
using (Stream requestStream = akamaiRequest.GetRequestStream())
{
requestStream.Write(byteArray, 0, byteArray.Length);
}
using (HttpWebResponse response = (HttpWebResponse)akamaiRequest.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}
}
Note: The highlighted auth value is nothing but the authorization header, the one we got from EdgeGridV1Signer - sign method .
If the web request is successful you will see the response like below:
{
"detail": "Request accepted",
"estimatedSeconds": 5,
"purgeId": "4dbff464-0283-11ea-a725-xxx6",
"supportId": "17PY1573257355655570-xxx7184",
"httpStatus": 201
}
There will be a status check page in Akamai v2 API, but thats not available in Akamai fast purge v3.