Introduction
First retrieve a token:
C# Example
string Authenticate()
{
string token = null;
if (String.IsNullOrEmpty(username) || String.IsNullOrEmpty(password) || String.IsNullOrEmpty(companyID))
{
return "Credentials missing";
}
FormUrlEncodedContent content = new FormUrlEncodedContent(new[]
{
new KeyValuePair
("grant_type", "password"),
new KeyValuePair
("companyId", companyID),
new KeyValuePair
("username", username),
new KeyValuePair
("password", password)
});
var baseURL = "{{BASE_URL}}/token";
var request = new HttpRequestMessage(HttpMethod.Post, baseURL);
request.Content = content;
HttpResponseMessage response = client.PostAsync(baseURL, content).Result;
if (response.IsSuccessStatusCode)
{
Task<Object>
res = response.Content.ReadAsAsync<Object>()
Console.WriteLine("Auth result: " + res);
Console.WriteLine("Auth result type: " + res.GetType());
Console.WriteLine("Auth result string: " + res.Result);
Newtonsoft.Json.Linq.JObject jObj = (Newtonsoft.Json.Linq.JObject)res.Result;
//get the ticjket out of the json object returned
token = jObj.GetValue("access_token").ToString();
Console.WriteLine("Auth token: " + token);
}
else
{
Console.WriteLine("Auth Was not successful: " + response.StatusCode);
output.Text = output.Text + "\r\nAuth Was not successful: " + response.StatusCode;
}
response.Dispose();
return token;
}
Javascript JQuery Example
var settings = {
"url": "{{BASE_URL}}/token",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/x-www-form-urlencoded"
},
"data": {
"grant_type": "password",
"companyId": "companyid goes here",
"username": "username goes here",
"password": "password goes here"
}
};
$.ajax(settings).done(function (response) {
console.log(response);
});
PHP Example
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => '%7B%7BBASE_URL%7D%7D/token',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'grant_type=password&companyId=COMPANY_ID&username=USERNAME&password=PASSWORD',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
The returned token may then be passed in the header of all following requests:
authorization: Bearer {token}