问题:Coinbase Advanced Trade API /api/v3/brokerage/products/<product_id>/ticker

问题描述 投票:0回答:1

问题:Coinbase 高级交易 API /api/v3/brokerage/products//ticker

出现 401 未经授权错误

我正在使用 Coinbase Advanced Trade API 构建交易机器人,但在尝试从 /api/v3/brokerage/products//ticker 端点获取实时市场数据时遇到 401 未经授权错误。

详情如下:

Setup:
    Using the coinbase-advancedtrade-python SDK (EnhancedRESTClient).
    API key format: organizations/{org_id}/apiKeys/{key_id}.
    The key is loaded correctly, and the bot successfully connects to the Coinbase API (e.g., fetching account balances and tradable pairs works).

Authentication:
    Authorization header:

Authorization: Bearer organizations/{org_id}/apiKeys/{key_id}

IP address is whitelisted.
The API key has permissions for View and Trade.

问题:

Fetching live ticker data returns:

    HTTPError: 401 Client Error: Unauthorized for url: https://api.coinbase.com/api/v3/brokerage/products/BTC-GBP/ticker
    Response Content: Unauthorized

    This happens for all product pairs (e.g., BTC-GBP, ETH-GBP).

Troubleshooting Steps:
    Verified the API key format and permissions.
    Manually tested the API endpoint using curl, with the same 401 Unauthorized error.
    Checked that the IP address is correctly whitelisted in the API key settings.
    Tried generating signed requests using the private key, but the error persists.

Question:
    Has anyone successfully used the /api/v3/brokerage/products/<product_id>/ticker endpoint for live market data?
    Are there additional permissions or authentication steps required for this endpoint?
    Does the Coinbase Advanced Trade API require specific headers or signing for this endpoint?

任何见解或工作实施示例将不胜感激!

尝试了一切都必须求助于 v2,但我需要实时报价。

python coinbase-api
1个回答
0
投票

不知道这是否可以帮助您,但这是我用来从 coinbase api 获取数据的代码。该文档称:“CDP API 密钥用于为 API 生成 JSON Web 令牌 (JWT)。生成 JWT 后,将其设置为授权承载标头以发出经过身份验证的请求。” :https://docs.cdp.coinbase.com/coinbase-app/docs/api-key-authentication

 from coinbase import jwt_generator
import requests

# Initialize the client
api_key = "organizations/{ID/apiKeys/{ID}
api_secret = "-----BEGIN EC PRIVATE KEY-----\n{ID}\n-----END EC PRIVATE KEY-----\n"

request_method = "GET"
request_path = "/v2/accounts"

def make_coinbase_request(jwt_token, endpoint):
    """
    Make an authenticated request to Coinbase API using JWT token.
    """
    base_url = "https://api.coinbase.com"
    headers = {
        "Authorization": f"Bearer {jwt_token}",
        "Content-Type": "application/json"
    }
    
    response = requests.get(f"{base_url}{endpoint}", headers=headers)
    return response.json()

def main():
    jwt_uri = jwt_generator.format_jwt_uri(request_method, request_path)
    jwt_token = jwt_generator.build_rest_jwt(jwt_uri, api_key, api_secret)
    accounts = make_coinbase_request(jwt_token, "/v2/accounts")
    print("Accounts:", accounts)


if __name__ == "__main__":
    main()
© www.soinside.com 2019 - 2024. All rights reserved.