How To: Request an Access Token for Autodesk APIs with Node.js

In this post I’ll demonstrate how to set up a simple Node.js server that will return an access token for Autodesk’s Web Service APIs. Every time you run the app, you’ll get a response in your console that contains the token.

Authentication API

Authentication API(link goes to official documentation) takes a POST request that contains your client id and secret in the request body. It sends you an access token that would expire in 1799 seconds – that is 30 minutes, to perform operations with Autodesk’s Web Service APIs.

The request:

POST /authentication/v1/authenticate HTTP/1.1
Host: developer.api.autodesk.com
Content-Type: application/x-www-form-urlencoded
client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=client_credentials

The response body should look like this:

{
  "token_type": "Bearer",
  "expires_in": 1799,
  "access_token": "Ttj3qdRiNRGQsu975PPXF578hzmP"
}

Now that we understood what the API does, let’s get started! If you’d like to skip the tutorial, all the code is in a GitHub repo.

1. Setup

First create a new folder for the code. cd into that folder using terminal/command line.

Node’s HTTPS APIs are the same as HTTP but over TLS/SSL. The OAuth request has to be sent via HTTPS or Autodesk servers will spit an error.

2. The code

Create a file called app.js and paste the following code:

var https = require("https");

/* change these variables with your own client id and secret */
var clientId = "YOUR CLIENT ID";
var clientSecret = "YOUR CLIENT SECRET";

/* send the POST request to Autodesk and print the response to console */
function getToken() {
	var requestBody = "client_id=" + clientId + "&client_secret=" + clientSecret + "&grant_type=client_credentials";

	var options = {
		host: "developer.api.autodesk.com",
		path: "/authentication/v1/authenticate",
		method: "POST",
		headers: {
			"Content-Type": "application/x-www-form-urlencoded"
		}
	};

	var authRequest = https.request(options, function (authResponse) {
		var responseString = "";

		authResponse.on('data', function (data) {
			responseString += data;
		});
		authResponse.on("end", function () {
			console.log(responseString); // print token response to console
		});
	});

	authRequest.write(requestBody);
	authRequest.end();
}

getToken();

3. Run the code

While in the directory, type node app.js to run. When getToken() is invoked, it will print the response body to the console.

Download the app on GitHub.

Possible error response messages and solutions:

{
   "developerMessage":"The client_id specified does not have access to the api product",
   "userMessage":"",
   "errorCode":"AUTH-001",
   "more info":"http://developer.api.autodesk.com/documentation/v1/errors/AUTH-001"
}

Reason: You didn’t use the right client id and secret.
Solution: Go to the My Apps page on Autodesk Developers Portal and make sure you copied and pasted the correct keys.

{
   "fault":{
      "faultstring":"Unable to identify proxy for host: developer.api.autodesk.com:80 and url: \/authentication\/v1\/authenticate",
      "detail":{
         "errorcode":"messaging.adaptors.http.configuration.ApplicationNotFound"
      }
   }
}

Reason: You used HTTP protocol to request an API.
Solution: Use HTTPS protocol to make OAuth calls.

Other possible errors can be found in this documentation page.

One thought on “How To: Request an Access Token for Autodesk APIs with Node.js”

Leave a Reply

Your email address will not be published. Required fields are marked *