POLONIEX API Connect

时间:2018-01-27 05:03:48

标签: api powershell poloniex

我正在尝试使用PowerShell连接到POLONIEX API。我尝试了以下代码的不同变体而没有任何运气。任何人都可以看看我错过了什么?

[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

非常感谢您提供任何帮助!!!

1 个答案:

答案 0 :(得分:2)

那是一个很好的旅程;搜索了一半的网络,学到了很多,这就是我想出的。可以从https://pastebin.com/iuezwGRZ采用更多功能。

如果你也在这里分享你的最终剧本会很棒。

$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