/
get_token.py
70 lines (58 loc) · 1.81 KB
/
get_token.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import datetime
import json
from Cryptodome.Signature import pkcs1_15
from Cryptodome.Hash import SHA256
from Cryptodome.PublicKey import RSA
import requests
import base64
import time
from normal_json import normalizer_jason
mil_time = int(time.time() * 1000)
url = "https://sandboxrc.tax.gov.ir/req/api/self-tsp/sync/GET_TOKEN/" # API url
headers= {'requestTraceId':f'{mil_time}', 'timestamp': f'{mil_time}', 'Content-Type':'application/json'}
dict_headers= {'requestTraceId':f'{mil_time}', 'timestamp': f'{mil_time}'}
dict_packet= {
"uid":'null',
"packetType":"GET_TOKEN",
"retry":'false',
"data":{"username":"Axxxxx"},
"encryptionKeyId":"",
"symmetricKey":"",
"iv":"",
"fiscalId":"",
"dataSignature":""
}
def get_tocken():
dict3 = {**dict_headers, **dict_packet} # Merging two dictionaries
print("dict3=>>>",dict3)
normalize = normalizer_jason(dict3)
key = RSA.import_key(open('rsa.private').read()) # Get the private key
h = SHA256.new(normalize.encode('utf-8'))
signature = pkcs1_15.new(key).sign(h) # Sign the packet
base64_bytes = base64.b64encode(signature)
base64_signature = base64_bytes.decode('ascii')
null = None
false = False
paket_for_send ={
"time": 1,
"packet": {
"uid": null,
"packetType": "GET_TOKEN",
"retry": false,
"data": {
"username": "Axxxxx"
},
"encryptionKeyId": "",
"symmetricKey": "",
"iv": "",
"fiscalId": "",
"dataSignature": ""
},
"signature": f"{base64_signature}"
}
r= requests.post(url, data= json.dumps(paket_for_send), headers = headers ,verify=False)
print(r)
print("responce==", r.text)
return json.loads(r.text)['result']['data']['token'] , json.loads(r.text)['timestamp']
x= get_tocken()
print('Tocken: ',x)