Basic Authentication
Function Description:
The most simple way to deal with authentication is to use HTTP basic authentication. We use a special HTTP header where we add 'username:password' encoded in base64.
Note that even though your credentials are encoded, they are not encrypted!
Construct the authorization header
If you need, you can construct and send the basic authorization header yourself as follows:
1. Build a string of the form username:password.
2. Encode the string to Base64.
3. Supply an authorization header with format Authorization: {encoded-string}. Make sure to replace {encoded-string} with your encoded string from Step 2.
Resource Information:
Method | GET |
---|---|
URL | .../login |
Requires authentication? | Yes |
Method Parameters:
Description of the headers parameter.
Parameter | Type | Required | Description |
---|---|---|---|
Authorization | string | Yes | A Basic Auth authorization string is composed of a base64-encoded string containing the username and password separated by a colon. Simple example: 1. Auth string (before encoding in base64): myUsername:myPassword 2. Auth string (after base64 encoding): bXlVc2VybmFtZTpteVBhc3N3b3Jk 3. Complete authorization header: Authorization: bXlVc2VybmFtZTpteVBhc3N3b3Jk |
Response Parameters:
A JSON string representing an array of objects with the following fields:
Field | Type | Description |
---|---|---|
status | boolean | Return true for success and null for failure |
value | string | If the value of status is true,it returns "{"token": "<Encode user name and password string in base64 encoding>"}"; |
Request Example:
http://localhost:8090/TotalControl/v1/login
Response Example:
If this API executes successfully, it returns:
{
"status": true,
"value": {
"token": "270eq7lXQK8bXYsJ"
}
}
Example:
RingoJS Example:
// Import the packages
var {request} = require("ringo/httpclient");
var base64 = require("ringo/base64");
// The username and password are separated by a single : and sent on the wire base64 encoded
var userpass = base64.encode("sigma:3D391497");
// Get the API token
var gettoken = request({
method: "GET",
url: "http://localhost:8090/TotalControl/v1/login",
headers: {
"Authorization": userpass
}
});
var ret_token = JSON.parse(gettoken.content);
print("Get the token,Return value: " + gettoken.content);
var token = ret_token.value.token;
print("//The value of token is:" + token);
The running result of the RingoJS:
If it executes successfully, it will return:
Get the token,Return value: {"status":true,"value":{"token":"veLZf8EL7gUoyFOL"}}
//The value of token is:veLZf8EL7gUoyFOL
Python Example:
#!/user/bin/python
#-*- coding:utf-8 -*-
import base64
import requests
# The username and password are separated by a single : and sent on the wire base64 encoded
user_pass = 'sigma:3D391497'
encodeStr = base64.b64encode(user_pass.encode("UTF-8"))
# Get the API token
LoginUrl = "http://localhost:8090/TotalControl/v1/login"
response = requests.get(LoginUrl, headers={'Authorization':encodeStr})
print("Get the token,Return value: ",response.json())
token = response.json()['value']['token']
if token is not None:
print("The value of token is: ", token)
else:
print("Failed to get token.")
The running result of the python:
If it executes successfully, it will return:
Get the token,Return value: {'status': True, 'value': {'token': 'SPQxpZO2Qz79oFWG'}}
The value of token is: SPQxpZO2Qz79oFWG