Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Powershell - API Authentication Issue #209

Open
BMan-Shell opened this issue Jan 28, 2025 · 1 comment
Open

Powershell - API Authentication Issue #209

BMan-Shell opened this issue Jan 28, 2025 · 1 comment

Comments

@BMan-Shell
Copy link

BMan-Shell commented Jan 28, 2025

I have two scripts written in powershell and basically one authenticates perfectly and responds with the markets and the other calling for balances is getting an authentication error using the same code call for the signature.

I created and API Key that was Read & Write for all three sections also.

If you would like to test the code it simply needs your own APIKey and APISecret for each script and send it.

####################### Script #1 #######################

# Set your API key and secret
$ApiKey = "<Your Key>"
$ApiSecret = '<Your Secret>'


# BTC Markets API Base URL
$BaseUrl = "https://api.btcmarkets.net"


# Function to create the API authentication signature
function Get-Signature {
    param (
        [string]$HttpMethod,
        [string]$Path,
        [string]$Timestamp,
        [string]$ApiSecret
    )

    Write-Host "   --- Executing Get-Signature ---"

    $Payload = "$HttpMethod$Path$Timestamp"
    $KeyBytes = [Convert]::FromBase64String($ApiSecret)
    $PayloadBytes = [System.Text.Encoding]::UTF8.GetBytes($Payload)
    $HashBytes = [System.Security.Cryptography.HMACSHA256]::New($KeyBytes).ComputeHash($PayloadBytes)
    [Convert]::ToBase64String($HashBytes)
}
# Generate the signature
#$Signature = Get-Signature -HttpMethod $HttpMethod -Path $Path -Timestamp $Timestamp -ApiSecret $ApiSecret


# Function to fetch market data
function Get-Markets {
    Write-Host "--- Executing Get-Markets ---"
    
    $Timestamp = [int64]([DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds())
    $Path = "/v3/markets"
    $HttpMethod = "GET"

    # Generate the signature
    $Signature = Get-Signature -HttpMethod $HttpMethod -Path $Path -Timestamp $Timestamp -ApiSecret $ApiSecret
    
    # Headers
    $Headers = @{
        "BM-AUTH-APIKEY" = $ApiKey
        "BM-AUTH-TIMESTAMP" = $Timestamp
        "BM-AUTH-SIGNATURE" = $Signature
    }
    $Headers

    # Send the request
    $Response = Invoke-RestMethod -Uri "$BaseUrl$Path" -Method $HttpMethod -Headers $Headers
    
    return $Response
}

# Execute the function
Get-Markets



####################### Script #2 #######################


# Set your API key and secret
$ApiKey = "<Your Key>"
$ApiSecret = '<Your Secret>'


# BTC Markets API Base URL
$BaseUrl = "https://api.btcmarkets.net"


# Function to create the API authentication signature
function Get-Signature {
    param (
        [string]$HttpMethod,
        [string]$Path,
        [string]$Timestamp,
        [string]$ApiSecret
    )

    Write-Host "   --- Executing Get-Signature ---"

    $Payload = "$HttpMethod$Path$Timestamp"
    $KeyBytes = [Convert]::FromBase64String($ApiSecret)
    $PayloadBytes = [System.Text.Encoding]::UTF8.GetBytes($Payload)
    $HashBytes = [System.Security.Cryptography.HMACSHA256]::New($KeyBytes).ComputeHash($PayloadBytes)
    [Convert]::ToBase64String($HashBytes)
}
# Generate the signature
#$Signature = Get-Signature -HttpMethod $HttpMethod -Path $Path -Timestamp $Timestamp -ApiSecret $ApiSecret


# Function to fetch account balances
function Get-Balances {
    Write-Host "--- Executing Get Balances ---"
    
    $Timestamp = [int64]([DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds())
    $Path = "/v3/accounts/me/balances"
    $HttpMethod = "GET"

    # Generate the signature
    $Signature = Get-Signature -HttpMethod $HttpMethod -Path $Path -Timestamp $Timestamp -ApiSecret $ApiSecret
    
    # Headers
    $Headers = @{
        "BM-AUTH-APIKEY" = $ApiKey
        "BM-AUTH-TIMESTAMP" = $Timestamp
        "BM-AUTH-SIGNATURE" = $Signature
    }
    $Headers # Outing headers

    # Send the request
    try {
        $Response = Invoke-RestMethod -Uri "$BaseUrl$Path" -Method $HttpMethod -Headers $Headers -Verbose

        return $Response
    } catch {
        Write-Error "Failed to fetch balances: $_"
    }
}

# Execute the function
Get-Balances


@martin-nginio
Copy link
Contributor

martin-nginio commented Feb 4, 2025

Hi @BMan-Shell

Thanks for reporting this issue. To start with it'd be great to update the SHA algorithm to use sha512 as per below example code in node.js here:
https://docs.btcmarkets.net/#section/Authentication/Sign-the-Message

We cover sample code for many programming languages and to further debug your application please run any of the samples and try to reproduce the same output in PowerShell (please make sure to use the same timestamps in the input).

Below you can find sample codes in a few programming languages including .NET and also Python.
https://docs.btcmarkets.net/#section/API-Client-Libraries

In general, there are not many API customers who use PowerShell for trading applications hence we have not invested in creating an official sample/demo code.

If you are keen to make PowerShell works and are interested to share your integration with other community members, we are happy to link your open source sample PowerShell project in our API docs so other people can use.

If possible, we encourage you to consider using Python or other languages that provide more flexibility and perhaps are more mainstream for this sort of integration. Choosing those languages (e.g. creating a Python app and then run it via command line) has the benefit of having larger support community and perhaps easier for integration (e.g. if you decide to use WebSocket then it will be perhaps even more difficult in PowerShell).

We will be adding a sample in Rust language very soon and perhaps that may also help also.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants