Ejemplos de uso

CURL

curl --location --request POST 'https://auth.prod.ua.la/1/auth/token' \
--header 'Content-Type: application/json' \
--data-raw '{
"user_name" : "[[user_name]]",
"client_id" : "[[client_id]]",
"client_secret_id" : "[[client_secret_id]]",
"grant_type" : "client_credentials"
}'

Python

#!/usr/bin/env/python
# -*- coding: utf-8 -*-
import json
try:
import requests
except ImportError:
print('[!] Please install the requests library')
print('[!] Example: pip install requests')
exit(1)
AUTH_URL = 'https://auth.prod.ua.la'
TOKEN_URL = f'{AUTH_URL}/1/auth/token'
class Auth:
def __init__(self, user_name: str, client_id: str, client_secret_id: str, grant_type: str) -> None:
self.user_name = user_name
self.client_id = client_id
self.client_secret_id = client_secret_id
self.grant_type = grant_type
def to_dict(self) -> dict:
return vars(self)
def to_json(self) -> str:
return json.dumps(self.to_dict())
def get_token(self):
try:
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
session = requests.Session()
response = session.post(
TOKEN_URL,
self.to_json(),
headers=headers)
return response.json()
except requests.exceptions.RequestException as e:
raise e('[!] Error while getting token')
if __name__ == '__main__':
auth = Auth(
user_name='USER_NAME',
client_id='CLIENT_ID',
client_secret_id='CLIENT_SECRET_ID_KEY',
grant_type='client_credentials'
)
response = auth.get_token()
print(json.dumps(response, indent=4))

Go

package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
const authUrl = "https://auth.prod.ua.la"
var tokenUrl = fmt.Sprintf("%s/1/auth/token", authUrl)
type (
// Auth struct
Auth struct {
UserName string `json:"user_name"`
ClientID string `json:"client_id"`
ClientSecretID string `json:"client_secret_id"`
GrantType string `json:"grant_type"`
}
// Token Response struct
Response struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
TokenType string `json:"token_type"`
}
TokenAuth interface {
GetToken() (Response, error)
}
)
func (auth *Auth) toJson() ([]byte, error) {
return json.Marshal(auth)
}
func (auth *Auth) GetToken() (Response, error) {
var token Response
data, err := auth.toJson()
if err != nil {
return token, err
}
resp, err := http.Post(tokenUrl, "application/json", bytes.NewBuffer(data))
if err != nil {
return token, err
}
defer resp.Body.Close()
if err = json.NewDecoder(resp.Body).Decode(&token); err != nil {
return token, err
}
return token, err
}
func main() {
auth := Auth{
UserName: "USER_NAME",
ClientID: "CLIENT_ID",
ClientSecretID: "CLIENT_SECRET_ID_KEY",
GrantType: "client_credentials",
}
token, err := auth.GetToken()
if err != nil {
fmt.Println(err)
} else {
fmt.Println(token)
}
}