Last active
December 30, 2015 07:49
-
-
Save hokamoto/7798563 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Collections.Specialized; | |
using System.Linq; | |
using System.Net; | |
using System.Security.Cryptography; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ConsoleApplication2 | |
{ | |
class Program | |
{ | |
private static string domain = "*********.luna.akamaiapis.net"; | |
private static string secret = "*********"; | |
private static string access_token = "********"; | |
private static string client_token = "********"; | |
static void Main(string[] args) | |
{ | |
string authorization_header = createAuthHeader("GET", "https", domain, "/alerts/v1/portal-user?status=active"); | |
using (WebClient client = new WebClient()) | |
{ | |
try | |
{ | |
client.Headers.Add("Authorization", authorization_header); | |
NameValueCollection qs = new NameValueCollection(); | |
qs.Add("status", "active"); | |
client.QueryString = qs; | |
string result = client.DownloadString("https://" + domain + "/alerts/v1/portal-user"); | |
Console.Write(result); | |
} | |
catch (WebException e) | |
{ | |
if (e.Status == WebExceptionStatus.ProtocolError) | |
{ | |
Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode); | |
Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription); | |
} | |
} | |
} | |
Console.ReadKey(); | |
} | |
static string CreateToken(string message, string secret) | |
{ | |
byte[] keyByte = Encoding.UTF8.GetBytes(secret); | |
byte[] messageBytes = Encoding.UTF8.GetBytes(message); | |
using (var hmacsha256 = new HMACSHA256(keyByte)) | |
{ | |
var hashmessage = hmacsha256.ComputeHash(messageBytes); | |
return Convert.ToBase64String(hashmessage); | |
} | |
} | |
static string createAuthHeader(string method,string scheme,string host,string path) | |
{ | |
Guid guidValue = Guid.NewGuid(); | |
string timestamp = DateTime.Now.ToUniversalTime().ToString("yyyyMMddTHH:mm:ss+0000"); | |
string nonce = guidValue.ToString(); | |
string signing_key = CreateToken(timestamp, secret); | |
string authorization_header = "EG1-HMAC-SHA256 "; | |
authorization_header += "client_token=" + client_token + ";"; | |
authorization_header += "access_token=" + access_token + ";"; | |
authorization_header += "timestamp=" + timestamp + ";"; | |
authorization_header += "nonce=" + nonce + ";"; | |
string[] dataToSign = { method, scheme, host, path, "", "", authorization_header }; | |
string signature = CreateToken(String.Join("\t", dataToSign).Trim(), signing_key); | |
authorization_header += "signature=" + signature; | |
return authorization_header; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment