In order to communicate with the API, the following steps must be followed.
- Generate an API key within TotalQA.
- Use the key to request an access token for the API
- Use the returned access token to use HTTP methods to communicate with the API (GET, POST, PATCH, etc)
Generating an API Key
Y
You must have administrative rights to generate API keys.
- In your TotalQA account, navigate to the "Manage - Application Programmer Interface" page.
- Click the +Add Token button and enter a descriptive name for your key.
A key will be generated and displayed on the main API page. You may generate multiple keys for your account. Each key must have a unique label.
- Click on the icon to the right of the key to copy to the clipboard.
Modifying API Keys
To modify an existing API key click on the Edit button next to the key and provide a new unique label. If you hover over the Edit button with the cursor, it will say Modify token. Note that the label is used when requesting access tokens to the service so changing the label may break existing code requesting access.
Deleting API Keys
To delete an API key click the Delete button next to the key. Note that any code using the key will not function once it is deleted.
Requesting an Access Token
The base URL for all API calls to TotalQA is https://tqa.imageowl.com/api/rest. In order to make a call to receive an access token. In order to request access to the API service send a POST request to tqa.imageowl.com/api/rest/oauth with the JSON body:
{
"client_id": "The descriptive id you set up above",
"client_secret": "The generated key string",
"grant_type": "client_credentials"
}
Note: the ID will be a concatenation of a number and the label you assigned. Copy the entire label (e.g 180:API_Connection)
MATLAB Example
%% Get the Access Token using the API Key we generated %this is the basic information needed r.client_id = 'label you provided'; r.client_secret = 'generated key'; r.grant_type = 'client_credentials'; %...make the request options = weboptions('MediaType','application/json','RequestMethod','post'); tokenRequestResponse = webwrite('https://tqa.imageowl.com/api/rest/oauth',r,options); %...display the authorization disp('The response to the access request'); disp(tokenRequestResponse);
Python Example
This example uses the Python requests package.
import json import requests class tqa_Connect: def demo(self): payload = {"client_id": "Your API Label", "client_secret": "Your API Key", "grant_type": "client_credentials" } r = requests.post('https://tqa.imageowl.com/api/rest/oauth',data = payload) print 'Status Code:',r.status_code j = r.json() accessToken = j['access_token'] print 'Access Token: ',j['access_token'] tqa_Connect().demo()
R Example
This example uses the R httr package.
library(httr) url <- 'http://tqa.imageowl.com/api/rest/oauth' body <- list(client_id = 'Your API Label', client_secret= 'Your API Key',grant_type = 'client_credentials') # get the access token in a respone r <- POST(url, body = body, encode = "form") tqa_token <- content(r,"parsed") # look at access token str(tqa_token)
C# (.NET) example
We need to establish some basic data contract classes to make the JSON formatting easier.
For some discussion using Data Contracts in .NET see Using Data Contracts and How to use HttpClient to post JSON data
For the request.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net.Http; using System.Net.Http.Headers; using System.Runtime.Serialization; namespace TQA_API_Demo { [DataContract] public class AccessRequest { [DataMember(Name = "client_id")] public String ClientID { get; set; } [DataMember(Name = "client_secret")] public String ClientSecret { get; set; } [DataMember(Name = "grant_type")] public String GrantType { get; set; } } }
And to store the response
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net.Http; using System.Net.Http.Headers; using System.Runtime.Serialization; namespace TQA_API_Demo { [DataContract] public class accessRequestResponseData { [DataMember(Name = "access_token")] public String AccessToken { get; set; } [DataMember (Name= "expires_in")] public int ExpiresIn { get; set; } } }
And then the call to get the access token.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net.Http; using System.Net.Http.Headers; using Newtonsoft.Json; // uses the Json.NET assembly a freely available JSON library using System.Runtime.Serialization; using System.Runtime.Serialization.Json; using System.IO; namespace TQA_API_Demo { class Program { static void Main() { RunAsync().Wait(); } static async Task RunAsync() { using (var client = new HttpClient()) { // TODO - Send HTTP requests Uri baseAddress = new Uri("https://tqa.imageowl.com/"); client.BaseAddress = baseAddress; client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var accessRequestData = new AccessRequest() { ClientID = "Your API Label", ClientSecret = "Your API Key", GrantType = "client_credentials" }; //HTTP get the access token HttpResponseMessage response = await client.PostAsJsonAsync("api/rest/oauth", accessRequestData); if (response.IsSuccessStatusCode) { accessRequestResponseData responseData = await response.Content.ReadAsAsync<accessRequestResponseData>(); Console.WriteLine("{0}\t{1}",responseData.AccessToken,responseData.ExpiresIn); } Console.ReadLine(); } } } }
Comments
0 comments
Please sign in to leave a comment.