API Sample Code (C#)
Here is sample code to access the API in C#:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Web;
using System.Xml;
namespace APITester
{
class Program
{
static void Main(string[] args)
{
string Login_User = "myusername@myemail.com";
string Login_Pass = "mypass123";
CRM_API myAPI = new CRM_API(Login_User, Login_Pass);
bool successFlag = myAPI.CRM_GetAuthToken();
if (successFlag)
{
Console.WriteLine("Auth Token was " + myAPI.Get_CRM_AuthToken());
}
else
{
Console.WriteLine("GetAuthToken failed");
}
}
}
class CRM_API
{
private string Login_User;
private string Login_Pass;
private string CRM_AuthToken;
private string m_API_URL;
private string strErrorMessage;
public CRM_API(string str_User, string str_pass)
{
Login_User = str_User;
Login_Pass = str_pass;
m_API_URL = "https://api.stgi.net/xml.pl";
}
public string Get_CRM_AuthToken()
{
return CRM_AuthToken;
}
public string Get_Error_Message()
{
return strErrorMessage;
}
public bool CRM_GetAuthToken()
{
try
{
String myxml = "<GetAuthTokenRequest></GetAuthTokenRequest>";
String str_input = "email=" + Uri.EscapeDataString(Login_User) + "&password=" + Uri.EscapeDataString(Login_Pass) + "&xml=" + Uri.EscapeDataString(myxml);
String str_output = "";
if (DoCRMAPICall(str_input, ref str_output))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(str_output);
XmlNode resultNode = xmlDoc.SelectSingleNode("/GetAuthTokenResponse/Result");
if (resultNode != null)
{
if (resultNode.InnerText.ToLower() == "success")
{
XmlNode tokenNode = xmlDoc.SelectSingleNode("/GetAuthTokenResponse/Token");
if (tokenNode != null)
{
CRM_AuthToken = tokenNode.InnerText;
return true;
}
else
{
strErrorMessage = "Could not find token in response";
return false;
}
}
else
{
XmlNode errorTextNode = xmlDoc.SelectSingleNode("/GetAuthTokenResponse/ErrorText");
if (errorTextNode != null)
{
strErrorMessage = errorTextNode.InnerText;
}
else
{
strErrorMessage = "Getting auth token from CRM failed with no error message found";
}
return false;
}
}
else
{
strErrorMessage = "Could not find result in CRM response";
return false;
}
}
else
{
strErrorMessage = "CRM API Call failed";
return false;
}
}
catch (Exception)
{
CRM_AuthToken = "";
return false;
}
}
private bool DoCRMAPICall(string str_input, ref string str_output)
{
StreamWriter myWriter = (StreamWriter)null;
StreamReader sr = (StreamReader)null;
try
{
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(m_API_URL);
UTF8Encoding encoding = new UTF8Encoding(false, true);
objRequest.Method = "POST";
objRequest.ContentLength = encoding.GetByteCount(str_input);
objRequest.ContentType = "application/x-www-form-urlencoded";
objRequest.Accept = "application/xml";
objRequest.KeepAlive = false;
myWriter = new StreamWriter(objRequest.GetRequestStream(), encoding);
myWriter.Write(str_input);
myWriter.Close();
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
sr = new StreamReader(objResponse.GetResponseStream());
str_output = sr.ReadToEnd();
return true;
}
catch (Exception e)
{
throw e;
}
finally
{
if (myWriter != (StreamWriter)null)
{
myWriter.Close();
}
if (sr != (StreamReader)null)
{
sr.Close();
}
}
}
}
}