简体   繁体   English

当连接到 coinbase(和 coinbase 沙箱)时收到 {'message': 'Invalid API Key'} 错误代码 Python

[英]when connecting to coinbase (and coinbase sandbox) getting {'message': 'Invalid API Key'} errors with Python code

I am using the following demo code (written in Python 3.x ) to attempt to connect to the Coinbase Sandbox.我正在使用以下演示代码(在Python 3.x中编写)尝试连接到 Coinbase 沙箱。 Below is the code I have been following.以下是我一直关注的代码。 I keep getting {'message': 'Invalid API Key'} errors.我不断收到{'message': 'Invalid API Key'}错误。 I have created API keys twice on the sandbox site: https://public.sandbox.pro.coinbase.com/ but nothing is working.我在沙盒网站上创建了两次 API 密钥: https://public.sandbox.pro.coinbase.com/但没有任何效果。

What am I doing wrong?我究竟做错了什么? Any help, hints or advice would be appreciated.任何帮助、提示或建议将不胜感激。

TIA TIA

import json, hmac, hashlib, time, requests, base64
from requests.auth import AuthBase

# Before implementation, set environmental variables with the names API_KEY and API_SECRET

APIKEY = 'XXXXXXX-API'
API_PASS = 'rXXd0XX8XX'
API_SECRET = b'h2IUKbXXXXXXXXXXKL2d9XXXXXXXXXXWde5u+zcXXXXXXXXXXXXGJ8YqD8TXXXXXXXXXXXXXXX3dqM8pXXXXX8w=='

# Create custom authentication for Exchange
class CoinbaseExchangeAuth(AuthBase):
    def __init__(self, api_key, secret_key, passphrase):
        self.api_key = api_key
        self.secret_key = secret_key
        self.passphrase = passphrase

    def __call__(self, request):
        timestamp = str(time.time())
        message = timestamp + request.method + request.path_url + (request.body or b'').decode()
        hmac_key = base64.b64decode(self.secret_key)
        signature = hmac.new(hmac_key, message.encode(), hashlib.sha256)
        signature_b64 = base64.b64encode(signature.digest()).decode()

        request.headers.update({
            'CB-ACCESS-SIGN': signature_b64,
            'CB-ACCESS-TIMESTAMP': timestamp,
            'CB-ACCESS-KEY': self.api_key,
            'CB-ACCESS-PASSPHRASE': self.passphrase,
            'Content-Type': 'application/json'
        })
        return request

api_url = 'https://api-public.sandbox.pro.coinbase.com/'
auth = CoinbaseExchangeAuth(APIKEY, API_SECRET,  API_PASS)


# Get accounts
r = requests.get(api_url + 'accounts', auth=auth)
print(r.json())
# [{"id": "a1b2c3d4", "balance":...

# Place an order
order = {
    'size': 1.0,
    'price': 1.0,
    'side': 'buy',
    'product_id': 'BTC-USD',
}
r = requests.post(api_url + 'orders', json=order, auth=auth)
print(r.json())

My mistake.我的错。 Was not using the right number for the API Key未使用正确的 API 密钥编号在此处输入图片说明

BTW, just for anyone else coming here: the Sandbox environment has DIFFERENT authorization keys, so you can't use the normal API Key, you have to create a new one.顺便说一句,对于来到这里的其他人:沙盒环境有不同的授权密钥,所以你不能使用普通的 API 密钥,你必须创建一个新的。 :) :)

I have had trouble finding a working solution in Python to get Coinbase Pro to authorize my API key I created.我在 Python 中找不到有效的解决方案来让 Coinbase Pro 授权我创建的 API 密钥。 The original posters code worked with my API key values and changing out of the Sandbox environment (I am not using Sandbox, for this question it is appropriate value).原始海报代码与我的 API 键值一起使用并改变了沙盒环境(我没有使用沙盒,对于这个问题它是适当的值)。

api_url = 'https://api.exchange.coinbase.com/'

Every other post I tried would give me stuff like:我试过的每一个其他帖子都会给我这样的东西:

key: expected bytes or bytearray, but got 'str'

I think this issue was resolved with the b in the line:我认为这个问题已通过行中的b解决:

API_SECRET = b'h2IUKbXXXXXXXXXXKL2d9XXXXXXXXXXWde5u+zcXXXXXXXXXXXXGJ8YqD8TXXXXXXXXXXXXXXX3dqM8pXXXXX8w=='

making it the expected bytes instead of str .使其成为预期的字节而不是str Thank you for this post.感谢您对这篇文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM