(This is a continuation from the previous post: Autodesk View and Data API Intro Overview)
In this lab, we learn the first step into the View and Data world and the basic of REST API call. This is a simple Windows Forms application written in C#. The View and Data services we use in this lab is authentication/authenticate, which is a call to obtain an authentication token required for subsequent calls.
To make REST calls, we use a library called RestSharp. We chose this as the syntax of RestSharp nicely reflects the semantics of REST call and makes it easy to read the code even if you aren’t writing in C#. Please refer to this post about how to include RestSharp library to your project.
Authenticate – Request
Authenticate creates and return an access_token, which is used for subsequent service calls.
Supported Request Methods: Post
client_id and client_secret are Consumer Key and Consumer Secret respectively, which you obtained from https://developer.autodesk.com/. If you haven’t gotten them, yet, please visit the site and apply for it. Anybody can apply.
(Note: When you request for keys, it will ask you to enter Callback URL. We do not use it in our exercise. You can enter any dummy value there.)
grant_type must be “client_credentials”.
Authenticate – Response
The response to View and Data API web services requests are in JSON format. A successful response to the authenticate request looks like below:
Note: line breaks are added for readability.
Authenticate – Sample Code
Here is a simple code to show the basic flow of authenticate request. Basically, what we are doing is to (1) fill out the information necessary to send a request, (2) make a web request, and (3) parse the response:
Now, let’s take a look at the code in more detail.
To make a client REST call, we first set the base URL:
var client = new RestClient();
client.BaseUrl = new System.Uri(baseApiUrl);
The base API URL is: https://developer.api.autodesk.com/
Typically, we define the base API URL, consumer key and secret as constants somewhere else, such as in the config file.
Next, set the request resource and method either Post or Get. e.g.,
var request = new RestRequest();
request.Resource = “authentication/v1/authenticate”;
request.Method = Method.POST;
Required parameters are set using AddParameter() method, e.g.,
Finally, call client.Execute(request) to get the response from the web service:
IRestResponse response = client.Execute(request);
Once you get a response, you can check the status of the response. If the status code indicates the success (i.e., OK), obtain header and response body info:
if (response.StatusCode == HttpStatusCode.OK) …
To parse the response, you can use JsonDeserializer class:
JsonDeserializer deserial = new JsonDeserializer();
AuthenticateResponse loginResponse =
accessToken = loginResponse.access_token;
where AuthenticateResponse is defined as:
These are the basic code that you need to make a View and Data API call.
Add UI to display an access token that we receive as a result of calling the above Authenticate() function. (A simple Win Form will serve out purpose for this.) As an example, your buttonToken_Click() might look something like this:
Disclaimer: as our goal is to learn View and Data API, we are not going into the detail of how to build UI, such as adding a button and text fields. You may choose not to add any UI to simplify your testing.
The image below shows a sample UI. (Note: In this sample, we added additional text fields, Request and Response, to show http request and response. This is for learning purpose and optional for your exercise. In your real application, you probably don’t show these to the user.)
When the authenticate call succeeds, you should receive access_token as a part of response. You will be using the access_token for subsequent calls to the View and Data API. You are ready for the next steps: a series of REST calls to upload a model for viewing.