PowerShell脚本RESTful API Binance URI结构

时间:2018-01-10 12:15:03

标签: rest api powershell

我尝试编写PowerShell脚本并使用https://api.binance.com/api/v3/order/test REST链接对其进行测试,但它不起作用。我无法理解我应该将什么用作消息,什么是身体以及什么作为标题。看来这里一切都很清楚,当我看到一个linux示例时,我应该在输出中有相同的链接: https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md 知道REST Post方法的人可以帮我弄清楚我应该在这里做些什么。

我的输出是: 发布https://api.binance.com/api/v3/order/test?symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=5000&timestamp=1515586306172&signature=LxHZUfC5MiTUfMPyEtgaVShlV1j4ITo3QxvtPAzPkwQ=

非常感谢提前。

function getLinks(target, source, debpth) {

    $.each($(source).children('a'), function () {

        var item = $('<li></li>');

        if (debpth) {


            item.append($("<a href='" + $(this).attr('href') + "'><i class='fa fa-circle fa-fw' aria-hidden='true' style='font-size:6px;vertical-align:middle;padding-right:15px'></i>" + $(this).html() + "</a>"));
        }
        else {
            item.append($("<a href='" + $(this).attr('href') + "'>" + $(this).html() + "</a>"));
        }
        item.addClass('debpth' + (debpth ? debpth : 0).toString());
        target.append(item);   

        var nested = getNestedLists($(this).siblings('ul'), debpth ? debpth : 0);
        if (nested) {
            $.each(nested, function () {

                $(this).children('li').addClass('debpth' + (debpth ? debpth : 0).toString());

                target.append($(this).children('li'));
            });

        }

    });

}

3 个答案:

答案 0 :(得分:0)

问题在于这行代码:

$query->bindParam(":starting_limit", $starting_limit, PDO::PARAM_INT); 

Binance希望HMAC SHA256以十六进制形式传输。用此替换上述内容,应解决问题。

$signature = [Convert]::ToBase64String($signature)

答案 1 :(得分:0)

下面是我的工作代码。 APIKey和Secret来自Binance API文档的示例 https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#signed-endpoint-examples-for-post-apiv1order

$ Signature的结果应与Binance示例相同:c8db56825ae71d6d79447849e617115f4a920fa2acdcab2b053c4b2838bd6b71

如果您使用自己的APIKey和Secret,则应该可以使用

$APIKey = "vmPUZE6mv9SD5VNHk4HlWFsOr6aKE2zvsw0MuIgwCIPy6utIco14y7Ju91duEh8A"
$APISecret = "NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j"

$TimeStamp = (Get-Date (Get-Date).ToUniversalTime() -UFormat %s).replace(',', '').replace('.', '').SubString(0,13)

$QueryString = "symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=5000&timestamp=1499827319559"

$hmacsha = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha.key = [Text.Encoding]::ASCII.GetBytes($APISecret)
$signature = $hmacsha.ComputeHash([Text.Encoding]::ASCII.GetBytes($QueryString))
$signature = [System.BitConverter]::ToString($signature).Replace('-', '').ToLower()

$uri = "https://api.binance.com/api/v3/account?$QueryString&signature=$signature"

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("X-MBX-APIKEY",$APIKey)   

try {

    Invoke-RestMethod -Uri $uri -Headers $headers -Method Get
}

Catch {

    $streamReader = [System.IO.StreamReader]::new($_.Exception.Response.GetResponseStream())
    $ErrResp = $streamReader.ReadToEnd() | ConvertFrom-Json
    $streamReader.Close()
    $ErrResp
}

答案 2 :(得分:0)

在您请求 Binance API 所需的所有功能下方。

参考:https://blog.p-difm.com/interact-with-your-binance-account-using-the-api-and-powershell/

function Get-UnixTimeStamp{
        <#
            .SYNOPSIS
            Return the timestamp in millisecond of the Unix Epoch
    
            .DESCRIPTION
            Unix Epoch started the Thursday, January 1, 1970 12:00:00 AM. The function return the number of second from that time.
    
            .EXAMPLE
            Get-UnixTimeStamp
            
        #>
        param(
    
        )
    
        return $(Get-Date (Get-Date).ToUniversalTime() -UFormat %s).replace(',', '').replace('.', '').SubString(0,13)
    }


function Get-BinanceAPISignature{
    <#
        .SYNOPSIS
        Prepare the signature that will be sent with the API request

        .DESCRIPTION
        Endpoint requires sending a valid API-Key and signature

        .PARAMETER QueryString
        The queryString must contains the symobol, timestamp and a recvWindow

        .PARAMETER EndPoint
        The EndPoint you want to request

        .EXAMPLE
        $URI = Get-BinanceAPISignature -QueryString $QueryString -EndPoint "/api/v3/openOrders"
        
    #>
    param(
        [Parameter(Mandatory=$true)]$QueryString,
        [Parameter(Mandatory=$true)]$EndPoint
    )
  
    $APISecret = "ASDHFASUHDFIOUSAHlUGLULUHALUHliuhalushduauauhIUH"

    $hmacsha     = New-Object System.Security.Cryptography.HMACSHA256
    $hmacsha.key = [Text.Encoding]::ASCII.GetBytes($APISecret)
    $signature   = $hmacsha.ComputeHash([Text.Encoding]::ASCII.GetBytes($QueryString))
    $signature   = [System.BitConverter]::ToString($signature).Replace('-', '').ToLower()

    $URI = "https://api.binance.com$($EndPoint)?$QueryString&signature=$signature"
    
    return $URI
}



function Get-BinanceAPIHeader{
    <#
        .SYNOPSIS
        Prepare the header that will be sent with the API request

        .DESCRIPTION
        The header include your APIKey

        .PARAMETER 
        #APIKey

        .EXAMPLE
        Get-BinanceAPIHeader
        
    #>
    param(
        
    )
  
    $APIKey = "HDAUSHF3989898hiuHGhuhI987898HiuahsduhaiusduhUIH"

    $Headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $Headers.Add("X-MBX-APIKEY",$APIKey)

    return $Headers
}



function Request-API{
    <#
        .SYNOPSIS
        Run the CURL command with defined parameters

        .DESCRIPTION
        Call the API and error handling. Return the result of the request

        .PARAMETER Method
        Choose a method according to the EndPoint

        .PARAMETER URI
        This parameter needs to be obtained with Get-BinanceAPISignature

        .PARAMETER Headers
        This parameter needs to be obtained with Get-BinanceAPIHeaderx

        .EXAMPLE
        $ObjResults = Request-API -Method Get -URI $URI -Headers $Headers
    #>

    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$true)][ValidateSet("POST","GET","DELETE")]$Method,
        [Parameter(Mandatory=$true)]$URI,
        [Parameter(Mandatory=$true)]$Headers
    )

    try{
        $ArrayJsonResult = Curl $URI -Method $Method -Headers $Headers #-Verbose
        $ObjCustomResult = $ArrayJsonResult.Content | ConvertFrom-Json
    }
    catch{
        $streamReader = [System.IO.StreamReader]::new($_.Exception.Response.GetResponseStream())
        $ErrResp = $streamReader.ReadToEnd() | ConvertFrom-Json
        $streamReader.Close()

        $LastError = $Error[0].ErrorDetails.Message | ConvertFrom-Json

        write-host "1: " $ErrResp -b Red
        write-host "2: " $LastError.code -b Red
        write-host "3: " $LastError.msg -b Red

        
        switch ($LastError.code){
            ("-1105") {write-host "TimeStamp issue"}
            ("-1003") {write-host "Too much request, IP Banned"; break}
            ("-2010") {write-host "Stop price would trigger immediately or Account has too many open stop loss and/or take profit orders on the symbol."}
            ("-1013") {write-host "The amount is not enough for this currency or not following the step size rule for the symbol."} 
            ("-1111") {write-host "Too many decimal check the precision required with Get-ExchangeInfo"}  
        }
    }
    
    return $ObjCustomResult
}

这里是一个如何使用它的例子 参考:https://blog.p-difm.com/new-order-with-api-binance-and-powershell/

function New-Order{
    <#
        .SYNOPSIS
        Place an order to buy or sell crypto

        .PARAMETER Symbol
        The crypto you want to buy or sell

        .PARAMETER Side
        ValidateSet "BUY" or "SELL"

        .PARAMETER OrderType
        ValidateSet "OrderMarket" or "OrderLimit" or "OrderStopLimit"

        .PARAMETER Quantity
        Specifies the amount you want to spend (when buying) or receive (when selling)

        .PARAMETER FiatAmount
        specifies the amount you want to spend in USDT 

        .EXAMPLE
        New-Order -Symbol BTCUSDT -Side BUY -OrderType OrderMarket -FiatAmount 20 -Verbose

        .EXAMPLE
        New-Order -Symbol BTCUSDT -Side BUY -OrderType OrderLimit -FiatAmount 1000 -Price 33000    
        
        .EXAMPLE
        New-Order -Symbol BTCUSDT -Side BUY -OrderType OrderStopLimit -Quantity 0.002 -Price 33000 -StopPrice 36000

        .EXAMPLE
        New-Order -Symbol XRPUSDT -Side SELL -OrderType OrderLimit -Quantity 100 -Price 0.5 
        
        .EXAMPLE
        New-Order -Symbol XRPUSDT -Side SELL -OrderType OrderStopLimit -Quantity 100 -Price 0.55 -StopPrice 0.5
    #>

    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$true)] 
        [string]$Symbol,

        [Parameter(Mandatory=$true)] 
        [ValidateSet("BUY", "SELL")]
        [string]$Side,

        [Parameter(Mandatory=$true)] 
        [ValidateSet("OrderMarket", "OrderLimit", "OrderStopLimit")]
        [string]$OrderType,

        [Parameter(Mandatory=$true, ParameterSetName = 'quantity')]
        [double]$Quantity,
    
        [Parameter(Mandatory=$true, ParameterSetName = 'quantity2')] 
        [double]$FiatAmount
    )

    DynamicParam{
        $paramDictionary = New-Object -Type System.Management.Automation.RuntimeDefinedParameterDictionary

        if ($OrderType -ne "OrderMarket"){
            # price param
            $attributes = New-Object -Type System.Management.Automation.ParameterAttribute
            $attributes.Mandatory = $true
            $attributeCollection = New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute]
            $attributeCollection.Add($attributes)

            $dynParam1 = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("Price", [double], $attributeCollection)
            $paramDictionary.Add("Price", $dynParam1)
        }
      
        if ($OrderType -eq "OrderStopLimit"){
            # StopPrice param
            $attributes2 = New-Object -Type System.Management.Automation.ParameterAttribute
            $attributes2.Mandatory = $true
            $attributeCollection2 = New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute]
            $attributeCollection2.Add($attributes2)

            $dynParam2 = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("StopPrice", [double], $attributeCollection2)
            $paramDictionary.Add("StopPrice", $dynParam2)
        }

        return $paramDictionary
    }

    BEGIN{
        # Check prerequisit
        try{
            Get-Command -Name Get-UnixTimeStamp -ErrorAction Stop | out-null
            Get-Command -name Get-BinanceAPISignature -ErrorAction Stop | Out-Null
            Get-Command -Name Get-BinanceAPIHeader -ErrorAction Stop | Out-Null
            Get-Command -Name Request-API -ErrorAction Stop | Out-Null
        }
        catch{
            Write-Host "Load Get-UnixTimeStamp, Get-BinanceAPISignature, Get-BinanceAPIHeader, Request-API  first prior to laod the current script" -b red
            Break
        }

        $TimeStamp = Get-UnixTimeStamp
        # retrieve value from dyn param
        if ($OrderType -ne "OrderMarket"){
            $Price     = $paramDictionary.values[0].value[0]
            $StopPrice = $paramDictionary.values[0].value[1]
        }

        switch($OrderType){
            "OrderMarket"{
                if($PSBoundParameters.ContainsKey('Quantity')){
                    $QueryString = "symbol=$Symbol&side=$Side&type=MARKET&quantity=$Quantity&timestamp=$TimeStamp&recvWindow=5000"
                }
                else{                                                                   
                    $QueryString = "symbol=$Symbol&side=$Side&type=MARKET&quoteOrderQty=$FiatAmount&timestamp=$TimeStamp&recvWindow=5000"
                }
            }
            
            "OrderLimit"{
                if($PSBoundParameters.ContainsKey('FiatAmount')){
                    $CurrentPrice = Get-Price -Symbol $Symbol -Decimal 8
                    $Quantity = [math]::Round($FiatAmount / $CurrentPrice, 6)
                }
                $QueryString = "symbol=$Symbol&side=$Side&type=LIMIT&price=$Price&timeInForce=GTC&quantity=$Quantity&timestamp=$TimeStamp&recvWindow=5000"
            }

            "OrderStopLimit"{
                if($PSBoundParameters.ContainsKey('FiatAmount')){
                    $CurrentPrice = Get-Price -Symbol $Symbol -Decimal 0
                    $Quantity = [math]::Round($FiatAmount / $CurrentPrice, 6)
                }
                $QueryString = "symbol=$Symbol&side=$Side&type=TAKE_PROFIT_LIMIT&stopPrice=$StopPrice&price=$Price&timeInForce=GTC&quantity=$Quantity&timestamp=$TimeStamp&recvWindow=5000"
            }
        }
    }
    
    PROCESS{
        $URI = Get-BinanceAPISignature -QueryString $QueryString -EndPoint "/api/v3/order"
        $Headers = Get-BinanceAPIHeader
        $ObjResults = $null # need to do this?
        $ObjResults = Request-API -Method POST -URI $URI -Headers $Headers
    }

    END{
       return $ObjResults
    }
}