import base64
import json
import requests
class GrpcTokenAuth:
def __init__(self,authUrl,clientId,clientSecret):
self.authUrl = authUrl
self.clientId = clientId
self.clientSecret = clientSecret
def header(self):
auth =self.clientId +":" +self.clientSecret
Authorization = base64.b64encode(auth.encode(encoding="utf-8")).decode()
_Authorization ="Basic " + Authorization
return _Authorization
def gettoken(self,rpc=None):
url =self.authUrl +"/api/v1/auth/oauth/token"
? ? ? ? header = {"Authorization":self.header(),
"Content-Type":"application/x-www-form-urlencoded"}
data = {"grant_type":"client_credentials"}
try:
res = requests.post(url,data=data,headers=header).json()
access_token = res["response"]["tokens"]["access_token"]
token_type = res["response"]["tokens"]["token_type"]
if rpc =='rpc':
rpc_token = access_token# grpc 使用
? ? ? ? ? ? ? ? params = {"client":self.clientId,"token": rpc_token}
print(f"grpc調(diào)用token:{json.dumps(params)}")
return params
else:
token = token_type +' ' + access_token# http 使用
? ? ? ? ? ? ? ? print(json.dumps(token))
print(f"http調(diào)用token:{token}")
return token
except Exception as e:
print(f"解析失敗:{res},異常信息:{e}")
if __name__ =='__main__':
# googlekey = 'GA3DANBVIBCDANCCIAYEARCDIA2DANBXGAYDANBZGAZEAQCEIBBA'
# PoloniexPassword.AuthenTicator(googlekey)
# GrpcTokenAuth().header()
? ? transfer_clientId ="x-referral"
? ? transfer_clientSecret ="x-referral-secret"
? ? spot_order_clientId ="spot-order"
? ? spot_order_clientSecret ="spot-order-secret"
? ? spot_admin_clientId ="spot-admin"
? ? spot_admin_clientSecret ="spot-admin-secret"
? ? spot_account_clientId ="spot-account"
? ? spot_account_clientSecret ="spot-account-secret"
? ? spot_asset_clientId ="spot-asset"
? ? spot_asset_clientSecret ="spot-asset-secret"
? ? spot_fee_clientId ="spot-feerate"
? ? spot_fee_clientSecret ="spot-feerate-secret"
? ? spot_archive_order_account_clientId ="spot-archive-order-account"
? ? spot_archive_order_account_clientSecret ="spot-archive-order-account-secret"
? ? spot_quotation_data_clientId ="spot-quotation-data"
? ? spot_quotation_data_clientSecret ="spot-quotation-data-secret"
? ? authUrl ="https://test-platform-auth.internal.poloniex.com"
? ? GrpcTokenAuth(authUrl,spot_account_clientId,spot_account_clientSecret).gettoken("rpc")