Created
February 26, 2014 14:58
-
-
Save danielbryantuk/9230942 to your computer and use it in GitHub Desktop.
Example Epoints Web Service (EWS) Client Connection Code in C#
This file contains 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.IO; | |
using System.Net; | |
using System.Security.Cryptography; | |
using System.Text; | |
//This is an example how to interact with Epoints API using C# Script execution engine (http://www.csscript.net/) | |
class Script | |
{ | |
static void Main() | |
{ | |
HttpWebRequest request = IatRequest( | |
"https://qa-api.epoints.com/transactions?action=getThisMonthSpent&apiKey=<YOUR_API_KEY>", | |
"<YOUR_API_KEY>", | |
"<YOUR_HMAC_KEY>" | |
); | |
WriteResponse(request.GetResponse() as HttpWebResponse); | |
} | |
static HttpWebRequest IatRequest(string url, string iatId, string secret) | |
{ | |
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; | |
request.Method = "GET"; | |
WebHeaderCollection headers = (request as HttpWebRequest).Headers; | |
string httpDate = DateTime.UtcNow.ToString("yyyyMMddHHmmssfff"); | |
headers.Add("x-iat-date", httpDate); | |
string canonicalString = "GET\n" + httpDate + "\n" + request.RequestUri.AbsolutePath; | |
KeyedHashAlgorithm hash = KeyedHashAlgorithm.Create("HmacSHA256"); | |
hash.Key = Encoding.UTF8.GetBytes(secret); | |
byte[] bytes = Encoding.UTF8.GetBytes(canonicalString); | |
byte[] signature = hash.ComputeHash(bytes); | |
string encodedSignature = Convert.ToBase64String(signature); | |
headers.Add("Authorization", "IAT " + iatId + ":" + encodedSignature); | |
return request; | |
} | |
static void WriteResponse(HttpWebResponse response) | |
{ | |
Stream stream = response.GetResponseStream() as Stream; | |
byte[] buffer = new byte[32 * 1024]; | |
int nRead =0; | |
MemoryStream ms = new MemoryStream(); | |
do | |
{ | |
nRead = stream.Read(buffer, 0, buffer.Length); | |
ms.Write(buffer, 0, nRead); | |
} while (nRead > 0); | |
ASCIIEncoding encoding = new ASCIIEncoding(); | |
string responseString = encoding.GetString(ms.ToArray()); | |
System.Console.WriteLine("\r\nHeaders:"); | |
for(int i=0; i < response.Headers.Count; ++i) | |
System.Console.WriteLine("{0} :{1}",response.Headers.Keys[i],response.Headers[i]); | |
System.Console.WriteLine("\nResponse:"); | |
System.Console.Write(responseString); | |
response.Close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment