As the login process is the first thing a scheduler does to access Snap Schedule 365, a client application must likewise present valid credentials to Snap Schedule 365. The difference between the two login processes is that a client application receives an access token, which is a unique value used to track the client application’s session with Snap Schedule 365.
Before a client application can use data from Snap Schedule 365, it must send a request including the company code, scheduler user name, and password to Snap Schedule 365. If these credentials are valid, Snap Schedule 365 will return an access token for the application to use in all subsequent requests. If the credentials are not valid, an access token is not returned and another request must be sent.
Requesting an Access Token
URL: POST /api/v1/account/requestauthorization
Content: None
Returns: An access token for use with all subsequent requests to Snap Schedule 365.
Releasing an Access Token
URL: GET /api/v1/account/releaseauthorization
Content: None
Returns: None
Example Code
public string GetAccessToken()
{
string accessToken = string.Empty;
var authorizationRequest = new
{
UserName = "SampleUser",
Password = "SamplePassword",
CompanyCode = "SampleCompany"
};
// Create web request to call API
var webRequest = (HttpWebRequest) WebRequest.Create(@"https://app.snapschedule365.com/api/v1/account/requestauthorization");
webRequest.Method = "POST";
webRequest.Accept = @"application/json";
webRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
// Add authorization request object to web request
byte[] authorizationRequestByteArray = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(authorizationRequest));
webRequest.ContentType = @"application/json";
webRequest.ContentLength = authorizationRequestByteArray.Length;
var dataStream = webRequest.GetRequestStream();
dataStream.Write(authorizationRequestByteArray, 0, authorizationRequestByteArray.Length);
dataStream.Close();
try
{
using (WebResponse webResponse = webRequest.GetResponse())
{
// If the web response is OK, then read the reply and extract the access token
if (( (HttpWebResponse) webResponse).StatusCode == HttpStatusCode.OK)
{
var reader = new StreamReader(webResponse.GetResponseStream());
dynamic authorizationResponse = JsonConvert.DeserializeObject<dynamic>(reader.ReadToEnd());
accessToken = authorizationResponse.AccessToken;
reader.Close();
}
}
}
catch (WebException e)
{
// An error occurred in the call -- handle appropriately
Console.WriteLine(e);
}
return accessToken;
}
function getAccessToken(callback)
{
// URL of API to invoke
var serviceUrl = "https://app.snapschedule365.com/api/v1/account/requestauthorization";
var requestContent = JSON.stringify(
{
UserName: "SampleUser",
Password: "SamplePassword",
CompanyCode: "SampleCompany"
});
// Create the request
var request = new XMLHttpRequest();
// Build the request
request.open("POST", serviceUrl, true);
request.setRequestHeader("Content-Type", "application/json");
// Set up request status handler to invoke the callback function when complete
request.onreadystatechange = function()
{
if (request.readyState == 4)
{
if (request.status == 200)
{
callback(JSON.parse(request.responseText).AccessToken);
}
else
{
alert("HTTP status: " + request.status + "\n" + request.responseText);
}
}
}
// Send the request, posting the content
request.send(requestContent);
}