Python 在coinbase api上请求传输/发送时出错
我试图将coinbase api从一个加密帐户转移到另一个加密帐户,但似乎无法实现。当我尝试传输2次时,错误出现在代码底部。第一次,它返回的是“{}”,第二次返回的是回溯错误 这是我的密码:Python 在coinbase api上请求传输/发送时出错,python,coinbase-api,Python,Coinbase Api,我试图将coinbase api从一个加密帐户转移到另一个加密帐户,但似乎无法实现。当我尝试传输2次时,错误出现在代码底部。第一次,它返回的是“{}”,第二次返回的是回溯错误 这是我的密码: import hmac, hashlib, time, requests, os from requests.auth import AuthBase from coinbase.wallet.client import Client API_KEY = os.environ.get('API_KEY')
import hmac, hashlib, time, requests, os
from requests.auth import AuthBase
from coinbase.wallet.client import Client
API_KEY = os.environ.get('API_KEY')
API_SECRET = os.environ.get('API_SECRET')
xrp_acct_id = os.environ.get('XRP_ID')
usdc_acct_id = os.environ.get('USDC_ID')
xrp_destination_tag = os.environ.get('xrp_destination_tag')
xtz_acct_id = os.environ.get('XTZ_ID')
user_id = os.environ.get('Coinbase_User_Id')
# Create custom authentication for Coinbase API
class CoinbaseWalletAuth(AuthBase):
def __init__(self, api_key, secret_key):
self.api_key = api_key
self.secret_key = secret_key
def __call__(self, request):
timestamp = str(int(time.time()))
message = timestamp + request.method + request.path_url + (request.body or b'').decode()
signature = hmac.new(bytes(self.secret_key, 'utf-8'), message.encode('utf-8'), hashlib.sha256).hexdigest()
request.headers.update({
'CB-ACCESS-SIGN': signature,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-KEY': self.api_key,
'CB-VERSION': '2020-01-18'
})
return request
api_url = 'https://api.coinbase.com/v2/'
auth = CoinbaseWalletAuth(API_KEY, API_SECRET)
client = Client(API_KEY, API_SECRET)
# Get current user:
# r_user = requests.get(api_url + 'user', auth=auth)
# print(r_user.json())
# Get transactions:
# xrp_txs = client.get_transactions(xrp_acct_id)
# Get accounts:
r = requests.get(api_url + 'accounts', auth=auth)
# Get price (XRP-USD):
price = client.get_spot_price(currency_pair='XRP-USD')
# To parse the JSON
accounts_json = r.json()
#
# # Variable to figure out wallet balances
XRP_balance = 0.0
# Dictionary used to store balances and wallet name's
accounts = {}
# Loop to get balances
for i in range(0, len(accounts_json["data"])):
wallet = accounts_json["data"][i]["name"]
amount = accounts_json["data"][i]["balance"]["amount"]
# Switch statement to figure out which index which wallet is
if wallet == "XRP Wallet":
XRP_balance = float(amount) * float(price["amount"])
accounts["XRP_USD"] = XRP_balance
elif wallet == "USDC Wallet":
USDC_amount = amount
accounts["USDC"] = USDC_amount
else:
print()
print(accounts)
txs = {
'type': 'transfer',
'account_id': usdc_acct_id,
'to': xrp_acct_id,
'amount': '10',
'currency': 'USDC'
}
r = requests.post(api_url + 'accounts/' + usdc_acct_id + '/transactions', json=txs, auth=auth)
print(r.json())
###### OUTPUT FROM THIS IS "{}" ##############
tx = client.transfer_money(account_id=usdc_acct_id,
to=xtz_acct_id,
amount='10',
fee='0.99',
currency='USDC')
###### OUTPUT FROM THIS IS "Traceback error" See Below ##############
txs = {
'type': 'send',
'to': xrp_address["address"],
'destination_tag': xrp_destination_tag,
'amount': '10',
'currency': 'XRP'
}
r = requests.post(api_url + 'accounts/' + xtz_acct_id + '/transactions', json=txs, auth=auth)
print(r)
print(r.json())
########## THIS OUTPUT IS FROM AFTER TRACEBACK ERROR ################
以下是整个输出:
{'USDC': '100.000000', 'XRP_USD': 571.5256683544001}
{}
Traceback (most recent call last):
File "/Users/mattaertker/Documents/CryptoExchangeProgram/exchangeMyCurrency.py", line 97, in <module>
tx = client.transfer_money(account_id=usdc_acct_id,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/coinbase/wallet/client.py", line 339, in transfer_money
response = self._post('accounts', account_id, 'transactions', data=params)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/coinbase/wallet/client.py", line 132, in _post
return self._request('post', *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/coinbase/wallet/client.py", line 116, in _request
return self._handle_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/coinbase/wallet/client.py", line 125, in _handle_response
raise build_api_error(response)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/coinbase/wallet/error.py", line 49, in build_api_error
blob = blob or response.json()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/models.py", line 898, in json
return complexjson.loads(self.text, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
<Response [400]>
{'errors': [{'id': 'validation_error', 'message': 'Please enter a valid email or Tezos address', 'field': 'base'}]}
输出:
<Response [201]>
我检查了我的tezos账户,当然因为回复是201,所以它确实发送给了自己。我最近发现,当您没有指定目的地标记时,它不会这样做,但仍然存在BUG 如果你想将密码从一种货币转换成另一种货币,你不必使用转账。从webapp查看您需要调用的端点Coinbase API:
GET”来了解它https://api.coinbase.com/v2/ /资产/价格?基础=美元&过滤器=可持有&分辨率=最新”,并从您的货币的“基础id”响应中获取
POST”下订单https://api.coinbase.com/v2/trade“
请求主体为json,如下所示:{'amount':[您要转换的金额],'amount\u asset':[您要转换的金额的货币],'amount\u from':'input','source\u asset':[您要出售的加密的“base\u id],'target\u asset':[您要购买的加密软件的“基本id”}
POST”https://api.coinbase.com/v2/trades/[上一次json响应的id]https://api.coinbase.com/v2/trade 发布“]/commit
。如果
此POST commit的响应代码为201,您的exchange为
已启动,如果coinbase中没有错误,则转换为
完成了
如果您想将加密货币从一种货币转换为另一种货币,则不必使用转账。从webapp观看您需要调用的端点Coinbase API:
GET”来了解它https://api.coinbase.com/v2/ /资产/价格?基础=美元&过滤器=可持有&分辨率=最新”,并从您的货币的“基础id”响应中获取
POST”下订单https://api.coinbase.com/v2/trade“
请求主体为json,如下所示:{'amount':[要转换的金额],'amount\u asset':[要转换的金额的货币],'amount\u from':'input','source\u asset':[要出售的加密的“base\u id],'target\u asset':[”您要购买的加密软件的基本id]}
POST”https://api.coinbase.com/v2/trades/[上一次json响应的id]https://api.coinbase.com/v2/trade 发布“]/commit
。如果
此POST commit的响应代码为201,您的exchange为
已启动,如果coinbase中没有错误,则转换为
完成了
<Response [201]>