How to get an API token and authorization in REST

This guide shows how to log in to an application using REST and get an API token to authenticate yourself in the following requests.

Requisites

Using a 3rd party tool

Obtaining the API token

To get the API token for a user, an HTTP POST request should be sent to the Token resource. In the post body, username and password are specified in JSON format, and the response body contains a token key with an actual API Token as the value. The token should be used in an HTTP Authorization header while communicating with other resources.

It is necessary to get a token using a tool (a browser or any other application that can send http requests). Depending on the client that is used, there are different ways to send HTTP headers. In this example, we show how to use Postman to send HTTP requests.

A login request consists of the following structure:

http://{hostname}:{port}/comGpsGate/api/v.1/applications/{applicationid}/tokens

  • {hostname} is where your GpsGate Server is installed.
  • {port} port number (the default port is 80).
  • {applicationid} is the ID of the application you are trying to log into.

You can find your application ID in Site Admin:

mceclip0.png

Example

  • Server IP: 192.168.0.34
  • Port: 80
  • Application ID: 40

Using the Postman tool, we'll make a request to the following URL

http://192.168.0.34:80/comGpsGate/api/v.1/applications/40/tokens where GpsGate is installed to the default port and the application Id 40.  

mceclip1.png

Authorize using the token

To send an authorization request to GpsGate REST API, you need to select the GET method with an authorization key (the token obtained previously), as shown in the image.

When the authorization is granted, the status code 200 should be returned by the system.

mceclip0.png

Using the GpsGate REST API GUI

Getting the token

To get the token, we will use the GpsGate helper tool. 

It is also possible to obtain the token using 3rd party tools, but that is not covered in this guide.

1. Go to https://<your server>/comGpsGate/api/v.1/test  where <your server> is your server URL. Note that you might have to enter HTTP or HTTPS at the beginning of the URL depending on your server configuration.

2. Go to the Tokens resources section and click to expand it.

mceclip0.png

3. Enter the applicationID, username and password, and click on Execute.

mceclip1.png

You will now see your authorization token in the results.

mceclip2.png

4. Copy/save this authorization key for later use.

Log in using the token

1. Go to the top of the URL https://<your server>/comGpsGate/api/v.1/test

2. Click on the top-right button Authorize

mceclip1.png

3. Paste the token ID generated above and click on Authorize

mceclip2.png

You will get a confirmation message

 mceclip3.png

Now you're logged into the system and can start using the rest of the resources available.