How to get an API token and authorization in REST
This guide shows how to log in to an application using REST API and get an authentication token using different tools to achieve it.
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:
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.
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.
Using the GpsGate REST API GUI
It is also possible to get the authentication token using REST API GUI without the need of using any other 3rd party tool. We will describe how to use the GUI in the following steps:
Getting the token
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.
3. Enter the applicationID, username and password, and click on Execute.
You will now see your authorization token in the results.
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
3. Paste the token ID generated above and click on Authorize
You will get a confirmation message
You're now logged into the system and can start using the rest of the resources available.