Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gitfvb
Last active August 17, 2018 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gitfvb/8c64682b876b8d3ecaa2fdcd32b7e4ce to your computer and use it in GitHub Desktop.
Save gitfvb/8c64682b876b8d3ecaa2fdcd32b7e4ce to your computer and use it in GitHub Desktop.
request the emarsys api together with Escher Authentication
# change this for the emarsys request
$username = "username001"
$secret = "secret"
$url = "https://trunk-int.s.emarsys.com/api/v2/field/translate/de"
#$url = "https://trunk-int.s.emarsys.com/api/v2/settings"
# authentication
<#
example for header
X-WSSE: UsernameToken
Username="customer001",
PasswordDigest="ZmI2ZmQ0MDIxYmQwNjcxNDkxY2RjNDNiMWExNjFkZA==",
Nonce="d36e316282959a9ed4c89851497a717f",
Created="2014-03-20T12:51:45Z"
source: https://dev.emarsys.com/v2/before-you-start/authentication
api endpoints: https://trunk-int.s.emarsys.com/api-demo/#tab-customer
other urls
https://dev.emarsys.com/v2/emarsys-developer-hub/what-is-the-emarsys-api
#>
# FUNCTIONS
function getRandomString([int] $length) {
$random = [Random]::new()
$chars = @("0", "2", "3", "4", "5", "6", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z")
$stringBuilder = ""
for ($i = 0; $i -lt $length; $i++) {
$stringBuilder += $chars[$random.Next($chars.Length)]
}
return $stringBuilder
}
# transform bytes into hexadecimal string (e.g. for hash values)
function getStringFromByte($byteArray) {
$stringBuilder = ""
$byteArray | ForEach { $stringBuilder += $_.ToString("x2") }
return $stringBuilder
}
function getSHA1($data) {
$hash = [System.Security.Cryptography.SHA1CryptoServiceProvider]::new()
return getStringFromByte -byteArray $hash.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($data))
}
# CREATE AUTH HEADER
$nonce = getStringfromByte(( GetRandomString -length 16 | Format-Hex ).Bytes)
$date = [datetime]::UtcNow.ToString("o")
$stringToSign = $nonce + $date + $secret
$sha1 = getSHA1 -data $stringToSign
$passwordDigest = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($sha1))
$xwsse = "UsernameToken Username=""$( $username )"", PasswordDigest=""$( $passwordDigest )"", Nonce=""$( $nonce )"", Created=""$( $date )""" # Content-type: application/json;charset=""utf-8"""
$xwsse
# CREATE REQUEST
$header = @{ "X-WSSE"=$xwsse; "Content-Type"="application/json" }
# EXECUTE AND SHOW REQUEST
$res = Invoke-RestMethod -uri $url -Method Get -Headers $header -Verbose
$res.data | Out-GridView
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment