I'm trying to connect to POLONIEX API using powershell. I've tried different variation of the following code without any luck. Could anyone take a look to see what I'm missing?
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$uri = 'https://poloniex.com/tradingApi'
$key = 'POLO-API-SECRET-KEY'
$secret= 'poloapisecret'
$nonce = [int][double]::Parse((Get-Date (get-date).touniversaltime() -UFormat %s))
$postParams = @{command="returnBalances";nonce="$nonce"}
### Encode URL and convert to raw bytes
#$utf8enc = New-Object System.Text.UTF8Encoding
#$postParams_bytes = $utf8enc.GetBytes($postParams)
$hmacsha = New-Object System.Security.Cryptography.HMACSHA512
$hmacsha.key = [Text.Encoding]::ASCII.GetBytes($secret)
$signature =
$hmacsha.ComputeHash([Text.Encoding]::ASCII.GetBytes($postParams))
$sign = [Convert]::ToBase64String($signature)
$headers2 = @{key="$key";sign="$sign"}
# $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
# $headers.Add("key",$key)
# $headers.Add("sign",$sign)
Invoke-WebRequest -Uri $uri -Method POST -Body $postParams -Headers $headers2 #| ConvertFrom-Json
BIG THANKS in advance for any help!!!
Well that was quite a ride; searched half the web, learned a lot and here is what I came up with. More functions may be adopted from https://pastebin.com/iuezwGRZ.
It would be great if you share your final script here also.
$PoloniexKey = "01234567-89ABCDEF-GHIJKLMN-OPQRSTUV"
$PoloniexSecret = "0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghij"
$TradingUri = 'https://poloniex.com/tradingApi'
$PublicUri = 'https://poloniex.com/public'
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# source: https://stackoverflow.com/questions/40680882/trying-to-make-hmac-sha256-work-with-powershell-for-canvas-api
function Buffer($string) {
$c=@()
Foreach ($element in $string.toCharArray()) {$c+= [System.Convert]::ToSByte($element)}
return $c
}
# source: https://www.remkoweijnen.nl/blog/2013/04/05/convert-bin-to-hex-and-hex-to-bin-in-powershell/
function BinToHex {
param(
[Parameter(
Position=0,
Mandatory=$true,
ValueFromPipeline=$true)
]
[Byte[]]$Bin)
# assume pipeline input if we don't have an array (surely there must be a better way)
if ($bin.Length -eq 1) {$bin = @($input)}
$return = -join ($Bin | foreach { "{0:X2}" -f $_ })
return $return.ToLower()
}
# source: https://gist.github.com/jokecamp/2c1a67b8f277797ecdb3
function HmacSHA512($Message, $Secret) {
$HMACSHA512 = new-object System.Security.Cryptography.HMACSHA512
$HMACSHA512.key = Buffer -string $Secret
$StringToHash = [System.Text.Encoding]::UTF8.GetBytes($Message)
$HashByteArray = $HMACSHA512.ComputeHash($StringToHash)
$hash = BinToHex $HashByteArray
return $hash;
}
function Invoke-PoloniexRequest($Request, $Uri=$TradingUri) {
$Nonce = ([int64](Get-Date (get-date).touniversaltime() -UFormat %s))*10
$Request.Add('nonce', $Nonce)
$RequestArray=@()
$Request.GetEnumerator() | %{$RequestArray += ($_.Name,$_.Value -join '=')}
$Body = $RequestArray -join '&'
$Sign = HmacSHA512 -Message $Body -Secret $PoloniexSecret
$Headers = @{
Key = $PoloniexKey
Sign = $Sign
}
Invoke-WebRequest -Uri $Uri -Method POST -Body $Body -Headers $Headers | ConvertFrom-Json
}
function Get-PoloniexBalances {
$request = @{command="returnBalances"}
return Invoke-PoloniexRequest -Request $request
}
$Balances = Get-PoloniexBalances