-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbs-get-transactions.py
151 lines (124 loc) · 5.13 KB
/
rbs-get-transactions.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import requests
import os
import urllib3
import re
import csv
import time
import sys
from tqdm import tqdm
BASE_PATH = os.path.dirname(os.path.abspath(__file__))
# Stop complaints about making unverified SSL requests.
urllib3.disable_warnings()
for environment in ['RBS_CLIENT_ID', 'RBS_CLIENT_SECRET']:
if environment not in os.environ:
print('Set {0} as an environment variable'.format(environment))
sys.exit(1)
# Update this configuration to match your environment.
CLIENT_ID = os.environ['RBS_CLIENT_ID']
CLIENT_SECRET = os.environ['RBS_CLIENT_SECRET']
APP_ID = '3a12066f-8853-444e-8e42-24670a35d9e8'
USERNAME = '1234567890|1234567890'
FINANCIAL_ID = '0015800000jfwB4AAI'
RBS_API_URL = 'https://ob.sandbox.rbs.co.uk'
REDIRECT_URI = 'https://{0}.example.org/redirect'.format(APP_ID)
print('Getting access token...')
r = requests.post(RBS_API_URL + '/token',
data={
'grant_type': 'client_credentials',
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'scope': 'accounts',
},
verify=False)
ACCESS_TOKEN = r.json()['access_token']
print('Getting consent...')
r = requests.post(RBS_API_URL + '/open-banking/v3.1/aisp/account-access-consents',
json={
'Data': {
'Permissions': [
"ReadAccountsDetail",
"ReadBalances",
"ReadTransactionsCredits",
"ReadTransactionsDebits",
"ReadTransactionsDetail",
],
},
'Risk': {},
},
headers={
'Authorization': 'Bearer {0}'.format(ACCESS_TOKEN),
'x-fapi-financial-id': FINANCIAL_ID,
},
verify=False)
CONSENT_ID = r.json()['Data']['ConsentId']
r = requests.get(RBS_API_URL + '/authorize',
params={
'client_id': CLIENT_ID,
'response_type': 'code id_token',
'scope': 'openid accounts',
'redirect_uri': REDIRECT_URI,
'request': CONSENT_ID,
'authorization_mode': 'AUTO_POSTMAN',
'authorization_result': 'APPROVED',
'authorization_username': '{0}@{1}.example.org'.format(USERNAME, APP_ID),
'authorization_accounts': '*',
},
verify=False)
AUTHORIZATION_CODE = re.search(r'#code=([^&]*)', r.json()['redirectUri']).group(1)
r = requests.post(RBS_API_URL + '/token',
data={
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'redirect_uri': REDIRECT_URI,
'grant_type': 'authorization_code',
'code': AUTHORIZATION_CODE,
},
verify=False)
RESOURCE_ACCESS_TOKEN = r.json()['access_token']
r = requests.get(RBS_API_URL + '/open-banking/v3.1/aisp/accounts',
headers={
'Authorization': 'Bearer {0}'.format(RESOURCE_ACCESS_TOKEN),
'x-fapi-financial-id': FINANCIAL_ID,
},
verify=False)
start = time.perf_counter()
for account in r.json()['Data']['Account']:
ACCOUNT_ID = account['AccountId']
print('Account id {0}'.format(ACCOUNT_ID))
page = 0
progress_bar = None
csv_file = os.path.join(BASE_PATH, 'output', 'transactions-{0}.csv'.format(ACCOUNT_ID))
with open(csv_file, 'w', newline='') as fh:
csv_writer = csv.writer(fh)
csv_writer.writerow([
'TransactionId',
'BookingDateTime',
'Amount',
'Currency',
])
while True:
r = requests.get(RBS_API_URL + '/open-banking/v3.1/aisp/accounts/{0}/transactions'.format(ACCOUNT_ID),
params={
'page': page,
},
headers={
'Authorization': 'Bearer {0}'.format(RESOURCE_ACCESS_TOKEN),
'x-fapi-financial-id': FINANCIAL_ID,
},
verify=False)
for transaction in r.json()['Data']['Transaction']:
csv_writer.writerow([
transaction['TransactionId'],
transaction['BookingDateTime'],
transaction['Amount']['Amount'],
transaction['Amount']['Currency'],
])
page += 1
total_pages = r.json()['Meta']['TotalPages']
if not progress_bar:
progress_bar = tqdm(total=int(total_pages))
progress_bar.update(1)
if page >= total_pages:
progress_bar.close()
break
print('Operation took {:,.2f} seconds'.format(time.perf_counter() - start))