C# voorbeeld script om een autorisatie-token samen te stellen.
public class Logic4API
{
public void GetProducts()
{
ApiSettings values = new ApiSettings
{
AdminstrationId = "1",
CompanyKey = "xxx",
PublicKey = "xxxxxxxxxxxxxxxxx",
SecretKey = "xxxxxxxxxxxxxxxxx",
ApiURL = "https://api.logic4.nl/",
Userid = 2000000
};
var task = GetProductsJson(GetHttpClient(values, "POST"));
var responseFromApi = task.Result;
}
public class ApiSettings
{
//Publickey verkregen via Logic4
public string PublicKey { get; set; }
//CompanyKey verkregen via Logic4
public string CompanyKey { get; set; }
//SecretKey verkregen via Logic4
public string SecretKey { get; set; }
//Vul hier het administratieID in, standaard is dit 1
public string AdminstrationId { get; set; }
//User bearer deze eigenschap is optioneel, wordt gebruikt voor api calls richting User eindpunten
public string UserBearer { get; set; }
//Vul hier de API URL in, vb: "https://api.logic4.nl/"
public string ApiURL { get; set; }
//Logic4 gebruikersID
public int Userid { get; set; }
}
public HttpClient GetHttpClient(ApiSettings _values, string requestMethod = "GET")
{
HttpClient client = new HttpClient();
//Als de bearer toevoegen aan de headers
if (!string.IsNullOrEmpty(_values.UserBearer))
{
client.DefaultRequestHeaders.Add("x-Logic4-userbearer", _values.UserBearer);
}
DateTime epochStart = new DateTime(1970, 01, 01, 0, 0, 0, 0, DateTimeKind.Utc);
TimeSpan timeSpan = DateTime.UtcNow - epochStart;
string requestTimeStamp = Convert.ToUInt64(timeSpan.TotalSeconds).ToString();
//create random nonce for each request
string nonce = Guid.NewGuid().ToString("N");
//Creating the raw signature string
string signatureRawData = String.Format("{0}{1}{2}{3}{4}",
_values.PublicKey, _values.CompanyKey, requestMethod, requestTimeStamp, nonce);
var secretKeyByteArray = Convert.FromBase64String(_values.SecretKey);
byte[] signature = Encoding.UTF8.GetBytes(signatureRawData);
using (HMACSHA256 hmac = new HMACSHA256(secretKeyByteArray))
{
byte[] signatureBytes = hmac.ComputeHash(signature);
string requestSignatureBase64String = Convert.ToBase64String(signatureBytes);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("X-LOGIC4-Authorization",
string.Format("{0}:{1}:{2}:{3}:{4}:{5}:{6}", _values.PublicKey, _values.CompanyKey, requestSignatureBase64String,
nonce, requestTimeStamp, _values.AdminstrationId, _values.Userid));
client.DefaultRequestHeaders.Date = DateTime.UtcNow;
}
return client;
}
public async Task
Voor PHP hebben we een voorbeeld script gemaakt om de autorisatie-token samen te stellen.
function GetAuthToken() {
$public_key = 'xxx';
$private_key = 'xxx';
$company_key = 'xxx';
$administration = 1;
$user_id = 2000000;
$timestamp = time();
$secret_key_byte_array = base64_decode($private_key);
$nonce = base64_encode($private_key.$timestamp);
$hashedstring = $public_key.$company_key.'GET'.$timestamp.$nonce;
$hash = base64_encode(hash_hmac('sha256', $hashedstring, $secret_key_byte_array, true));
$authToken = "{$public_key}:{$company_key}:{$hash}:{$nonce}:{$timestamp}:{$administration}:{$user_id}";
return $authToken;
}
function GetWebShopUserTypes() {
$client = new GuzzleHttp\Client([
'headers'=> [
'X-LOGIC4-Authorization' => GetAuthToken()
]
]);
$response = $client->request('GET', 'https://api.logic4.nl/Webshop/GetWebShopUserTypes');
var_dump((string)$response->getBody());
}
GetWebShopUserTypes();
Voor PowerShell hebben we een voorbeeld script gemaakt om de autorisatie-token samen te stellen.
$timestamp = [int64](([datetime]::UtcNow)-(get-date "1/1/1970")).TotalSeconds
$public_key = 'xxx';
$private_key = 'xxx';
$company_key = 'xxx';
$administration = 1;
$user_id = 2000000;
$nonceString = [System.Text.Encoding]::UTF8.GetBytes($private_key + $timestamp)
$nonce = [System.Convert]::ToBase64String($nonceString)
$hashedstring = $public_key + $company_key + "GET" + $timestamp + $nonce;
$hmacsha = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha.key = [System.Convert]::FromBase64String($private_key)
$hash = $hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($hashedstring))
$hash = [System.Convert]::ToBase64String($hash)
$authToken = "$($public_key):$($company_key):$($hash):$($nonce):$($timestamp):$($administration):$($user_id)";
Write-Output($authToken)