B
B
bluefasik2022-01-07 13:16:37
Python
bluefasik, 2022-01-07 13:16:37

How to fix an error when connecting Bitzlato Api?

import datetime
import http
import python_http_client
import time
import random
from jose import jws
from jose.constants import ALGORITHMS
# from bitzlato import Bitzlato

# secret user key
key =   {"kty":"EC","crv":"P-256","x":"KKKKKKKKKKS9REfefghTL_11sxrqA6s3OKQIgJ_BClM","y":"mQfffffeDDDDDFEEF-SpMa2kTJ1klw-op2NzgbtFaNg8","alg":"ES256"}


def main():
    dt = datetime.datetime.now()
    ts = time.mktime(dt.timetuple())
    claims = {
        # user identifier
        "email": "[email protected]",
        # leave as is
        "aud": "usr",
        # token issue time
        "iat": int(ts),
        # unique token identifier
        "jti": hex(random.getrandbits(64))
    }
    print(claims)
    # make token with claims from secret user key
    # put the correct key id (kid)
    token = jws.sign(claims, key, headers={"kid": "1"}, algorithm=ALGORITHMS.ES256)
    conn = http.client.HTTPSConnection("www.bitzlato.com", 443)

    # make  request against api endpoint with Authorization: Bearer <token> header
    res = conn.request("GET", "/api/auth/whoami", headers={
        "Authorization": "Bearer " + token
    })
    r1 = conn.getresponse()
    print(r1.status, r1.reason, r1.read())


if __name__ == '__main__':
    main()

Traceback (most recent call last):
File "C:\Users\BLueface\AppData\Local\Programs\Python\Python310\lib\site-packages\jose\jws.py", line 161, in _sign_header_and_claims
signature = key.sign (signing_input)
File "C:\Users\BLueface\AppData\Local\Programs\Python\Python310\lib\site-packages\jose\backends\cryptography_backend.py", line 154, in sign
signature = self.prepared_key.sign( msg, ec.ECDSA(self.hash_alg()))
AttributeError: 'DuringEllipticCurvePublicKey' object has no attribute 'sign'

handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users \BLueface\Desktop\parser\btcbitzlato.py", line 42, in
main()
File "C:\Users\BLueface\Desktop\parser\btcbitzlato.py", line 30, in main
token = jws.sign(claims, key, headers={"kid": "1"}, algorithm=ALGORITHMS.ES256 )
File "C:\Users\BLueface\AppData\Local\Programs\Python\Python310\lib\site-packages\jose\jws.py", line 43, in sign
signed_output = _sign_header_and_claims(encoded_header, encoded_payload, algorithm, key)
File "C:\Users\BLueface\AppData\Local\Programs\Python\Python310\lib\site-packages\jose\jws.py", line 163, in _sign_header_and_claims
raise JWSError(e)
jose.exceptions.JWSError: '_EllipticCurvePublicKey 'object has no attribute 'sign'

How to fix this error?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AidenTale, 2022-02-15
@AidenTale

You need to copy the API key to bitlato when creating it, not after. You are missing the dictionary key 'd'. Just create a new API and copy the key before sending it to the server.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question