Python
import os
from gcore import Gcore
client = Gcore(
api_key=os.environ.get("GCORE_API_KEY"), # This is the default and can be omitted
)
api_token_created = client.iam.api_tokens.create(
client_id=0,
client_user={},
exp_date="2021-01-01T12:00:00.000000Z",
name="My token",
)
print(api_token_created)package main
import (
"context"
"fmt"
"github.com/G-Core/gcore-go"
"github.com/G-Core/gcore-go/iam"
"github.com/G-Core/gcore-go/option"
)
func main() {
client := gcore.NewClient(
option.WithAPIKey("My API Key"),
)
apiTokenCreated, err := client.Iam.APITokens.New(
context.TODO(),
0,
iam.APITokenNewParams{
ClientUser: iam.APITokenNewParamsClientUser{},
ExpDate: gcore.String("2021-01-01T12:00:00.000000Z"),
Name: "My token",
},
)
if err != nil {
panic(err.Error())
}
fmt.Printf("%+v\n", apiTokenCreated)
}
curl --request POST \
--url https://api.gcore.com/iam/v2/clients/{clientId}/tokens \
--header 'Authorization: <api-key>' \
--header 'Content-Type: application/json' \
--data @- <<EOF
{
"name": "My token",
"exp_date": "2021-01-01T12:00:00.000000Z",
"client_user": {
"role": {
"id": 1,
"name": "Administrators"
}
},
"description": "It's my token",
"assign_default_policy": true
}
EOFconst options = {
method: 'POST',
headers: {Authorization: '<api-key>', 'Content-Type': 'application/json'},
body: JSON.stringify({
name: 'My token',
exp_date: '2021-01-01T12:00:00.000000Z',
client_user: {role: {id: 1, name: 'Administrators'}},
description: 'It\'s my token',
assign_default_policy: true
})
};
fetch('https://api.gcore.com/iam/v2/clients/{clientId}/tokens', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.gcore.com/iam/v2/clients/{clientId}/tokens",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'name' => 'My token',
'exp_date' => '2021-01-01T12:00:00.000000Z',
'client_user' => [
'role' => [
'id' => 1,
'name' => 'Administrators'
]
],
'description' => 'It\'s my token',
'assign_default_policy' => true
]),
CURLOPT_HTTPHEADER => [
"Authorization: <api-key>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}HttpResponse<String> response = Unirest.post("https://api.gcore.com/iam/v2/clients/{clientId}/tokens")
.header("Authorization", "<api-key>")
.header("Content-Type", "application/json")
.body("{\n \"name\": \"My token\",\n \"exp_date\": \"2021-01-01T12:00:00.000000Z\",\n \"client_user\": {\n \"role\": {\n \"id\": 1,\n \"name\": \"Administrators\"\n }\n },\n \"description\": \"It's my token\",\n \"assign_default_policy\": true\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.gcore.com/iam/v2/clients/{clientId}/tokens")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = '<api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"name\": \"My token\",\n \"exp_date\": \"2021-01-01T12:00:00.000000Z\",\n \"client_user\": {\n \"role\": {\n \"id\": 1,\n \"name\": \"Administrators\"\n }\n },\n \"description\": \"It's my token\",\n \"assign_default_policy\": true\n}"
response = http.request(request)
puts response.read_body{
"name": "My token",
"exp_date": "2021-01-01T12:00:00.000000Z",
"client_user": {
"role": {
"id": 1,
"name": "Administrators"
},
"deleted": false,
"user_id": 123,
"user_name": "John Doe",
"user_email": "some@email.com",
"client_id": 456
},
"id": 42,
"deleted": false,
"expired": false,
"created": "2021-01-01T12:00:00.000000Z",
"last_usage": "2021-01-01T12:00:00.000000Z",
"token": "42_a1b2c3d4e5f6...",
"description": "It's my token"
}{
"errors": {
"client_user": {
"role": [
"Invalid role: RoleName"
]
}
}
}API Tokens
Create API token
Create an API token in the current account.
This V2 endpoint generates tokens that use _ (underscore) as the separator
between the token ID and the secret string, instead of the $ separator
used by the V1 endpoint. For example: 42_a1b2c3d4...
Tokens created via this endpoint are fully compatible with all existing authentication mechanisms.
POST
/
iam
/
v2
/
clients
/
{clientId}
/
tokens
Python
import os
from gcore import Gcore
client = Gcore(
api_key=os.environ.get("GCORE_API_KEY"), # This is the default and can be omitted
)
api_token_created = client.iam.api_tokens.create(
client_id=0,
client_user={},
exp_date="2021-01-01T12:00:00.000000Z",
name="My token",
)
print(api_token_created)package main
import (
"context"
"fmt"
"github.com/G-Core/gcore-go"
"github.com/G-Core/gcore-go/iam"
"github.com/G-Core/gcore-go/option"
)
func main() {
client := gcore.NewClient(
option.WithAPIKey("My API Key"),
)
apiTokenCreated, err := client.Iam.APITokens.New(
context.TODO(),
0,
iam.APITokenNewParams{
ClientUser: iam.APITokenNewParamsClientUser{},
ExpDate: gcore.String("2021-01-01T12:00:00.000000Z"),
Name: "My token",
},
)
if err != nil {
panic(err.Error())
}
fmt.Printf("%+v\n", apiTokenCreated)
}
curl --request POST \
--url https://api.gcore.com/iam/v2/clients/{clientId}/tokens \
--header 'Authorization: <api-key>' \
--header 'Content-Type: application/json' \
--data @- <<EOF
{
"name": "My token",
"exp_date": "2021-01-01T12:00:00.000000Z",
"client_user": {
"role": {
"id": 1,
"name": "Administrators"
}
},
"description": "It's my token",
"assign_default_policy": true
}
EOFconst options = {
method: 'POST',
headers: {Authorization: '<api-key>', 'Content-Type': 'application/json'},
body: JSON.stringify({
name: 'My token',
exp_date: '2021-01-01T12:00:00.000000Z',
client_user: {role: {id: 1, name: 'Administrators'}},
description: 'It\'s my token',
assign_default_policy: true
})
};
fetch('https://api.gcore.com/iam/v2/clients/{clientId}/tokens', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.gcore.com/iam/v2/clients/{clientId}/tokens",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'name' => 'My token',
'exp_date' => '2021-01-01T12:00:00.000000Z',
'client_user' => [
'role' => [
'id' => 1,
'name' => 'Administrators'
]
],
'description' => 'It\'s my token',
'assign_default_policy' => true
]),
CURLOPT_HTTPHEADER => [
"Authorization: <api-key>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}HttpResponse<String> response = Unirest.post("https://api.gcore.com/iam/v2/clients/{clientId}/tokens")
.header("Authorization", "<api-key>")
.header("Content-Type", "application/json")
.body("{\n \"name\": \"My token\",\n \"exp_date\": \"2021-01-01T12:00:00.000000Z\",\n \"client_user\": {\n \"role\": {\n \"id\": 1,\n \"name\": \"Administrators\"\n }\n },\n \"description\": \"It's my token\",\n \"assign_default_policy\": true\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.gcore.com/iam/v2/clients/{clientId}/tokens")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = '<api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"name\": \"My token\",\n \"exp_date\": \"2021-01-01T12:00:00.000000Z\",\n \"client_user\": {\n \"role\": {\n \"id\": 1,\n \"name\": \"Administrators\"\n }\n },\n \"description\": \"It's my token\",\n \"assign_default_policy\": true\n}"
response = http.request(request)
puts response.read_body{
"name": "My token",
"exp_date": "2021-01-01T12:00:00.000000Z",
"client_user": {
"role": {
"id": 1,
"name": "Administrators"
},
"deleted": false,
"user_id": 123,
"user_name": "John Doe",
"user_email": "some@email.com",
"client_id": 456
},
"id": 42,
"deleted": false,
"expired": false,
"created": "2021-01-01T12:00:00.000000Z",
"last_usage": "2021-01-01T12:00:00.000000Z",
"token": "42_a1b2c3d4e5f6...",
"description": "It's my token"
}{
"errors": {
"client_user": {
"role": [
"Invalid role: RoleName"
]
}
}
}Authorizations
Body
application/json
Response
OK.
Was this page helpful?