0

我正在尝试使用 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

非常感谢您的帮助!!!

4

1 回答 1

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
于 2018-01-27T09:56:21.607 回答