首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >POLONIEX API连接

POLONIEX API连接
EN

Stack Overflow用户
提问于 2018-01-27 05:03:48
回答 1查看 292关注 0票数 0

我正在尝试使用powershell连接POLONIEX API。我尝试过以下代码的不同变体,但没有任何结果。有人能看看我错过了什么吗?

代码语言:javascript
复制
[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

非常感谢您的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-27 09:56:21

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

如果你也能在这里分享你的最终剧本,那就太好了。

代码语言:javascript
复制
$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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48472761

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档