Hilfe & Wissen Wie benutze ich die API?
Wie benutze ich die API?
Credentials
Als Credentials brauchen sie die client_id
einen kajomi usernamen
und das dazugehörige passwort
. Ihre client_id
ist exampleclient
Die Api ist unter https://nextgen.kajomigenerator.de/docs/ beschrieben. Es handelt sich um eine oAuth2 API die mittels Swagger
beschrieben ist. Unter der URL ist auch ein API Browser benutzbar. Als username
und passwort
können sie Ihren normalen Systembenutzer verwenden.
Login via oAuth2
Unter https://nextgen.kajomigenerator.de/oauth2/login?responsetype=token&redirecturi= kann man sich mittels client_id
, username
und passwort
einloggen. Nachdem alles via POST request an die oAuth2 URL geschickt, kommt ein 302 als erfolgreich zurück, und es wird an die redirecturi weitergeleitet. Bsp. Location: https://www.test.de#access_token=meinaccesstoken
oder für die obige URL Location: #access_token=meinaccesstoken
. Es kann natürlich hier auch sofort das accesstoken rausgeparst werden. Diese wird dann bei allen anderen API Calls im Authorization: Bearer [token]
Header verwendet. Das Token ist URL Encoded das sollte dann natürlich vor dem hinschicken decodiert werden.
Bsp. Code für das Login via oAuth2
<?php
class Kajomi {
public $apiBase = 'https://nextgen.kajomigenerator.de';
public $access_token = NULL;
public function authorize($client_id, $username, $password, $totp=NULL) {
$oAuthUrl = $this->apiBase . '/oauth2/login';
$payload = [
'client_id' => $client_id,
'username' => $username,
'password' => $password,
'response_type' => 'token'
];
$ch = curl_init($oAuthUrl);
curl_setopt($ch, CURLOPT_HEADER, 1);
if ($totp != NULL) {
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-OAUTH2-2FA: ' . $totp,
));
}
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload) );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($curl, $header) use (&$headers)
{
$len = strlen($header);
$header = explode(':', $header, 2);
if (count($header) < 2) // ignore invalid headers
return $len;
$headers[strtolower(trim($header[0]))][] = trim($header[1]);
return $len;
}
);
$res = curl_exec($ch);
curl_close($ch);
parse_str($headers['location'][0], $output);
if (!isset($output['/#access_token'])) {
throw new Exception('Something went wrong');
}
$this->access_token = $output['/#access_token'];
return true;
}
public function makeRequest($apiCall, $method="GET", $payload=NULL) {
if ($this->access_token == NULL) {
throw new Exception('NO AccessToken');
}
if ($method == "GET" && $payload != NULL) {
$apiCall . '?' . http_build_query($payload);
}
$ch = curl_init($this->apiBase . $apiCall);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $this->access_token,
));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($method == "POST" || $method == "PUT") {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_Setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload) );
}
$res = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if (!in_array($info["http_code"], [200,201,301,302])) {
throw new Exception("Error during Call: " . $res);
}
$responseContent = json_decode($res, true);
return $responseContent;
}
public function determineLastNewsletter($date=NULL) {
if ($date == NULL) {
$date = date('Y-m-d');
}
$payload = [
'from' => $date,
'to' => $date . ' 23:49'
];
$res = $this->makeRequest('/list/1/statistics', "GET", $payload);
return $res;
}
}
$test = new Kajomi();
$test->authorize('exampleclient', "john.doe", "ihrpasswort");
$test->determineLastNewsletter();