-
Notifications
You must be signed in to change notification settings - Fork 0
/
dl_query.py
66 lines (51 loc) · 1.68 KB
/
dl_query.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
import sys
import json
import csv
import logging
import param # From directory
import requests
import msal
from requests.structures import CaseInsensitiveDict
# Enter parameters here
par_authority = 'https://login.microsoftonline.com/<your-tenant>/'
par_client_cred = '<secret>'
par_client_id = '<client-id-or-app-id>'
par_odata = '<odata-link>'
par_outfile = 'query_data_from_py.csv'
app = msal.ConfidentialClientApplication(
client_id = par_client_id,
authority = par_authority,
client_credential = par_client_cred,
)
result = app.acquire_token_for_client("https://workplaceanalytics.office.com/.default")
if "access_token" in result:
headers = CaseInsensitiveDict()
headers["Accept"] = "application/json"
headers["Authorization"] = "Bearer "+result['access_token']
print(result['access_token'])
r = requests.get(
url = par_odata,
headers=headers )
print("Status code: ", r.status_code)
## TOGGLE FOR DEBUGGING ONLY
# file = open("resp_text.txt", "w", encoding = "utf-8")
# file.write(r.text)
data = json.loads(r.text)
person_data = data['value']
data_file = open(par_outfile, 'w', newline = '')
csv_writer = csv.writer(data_file)
count = 0
for p in person_data:
if count == 0:
# Writing headers of CSV file
header = p.keys()
csv_writer.writerow(header)
count += 1
# Writing data of CSV file
csv_writer.writerow(p.values())
data_file.close()
print("Query data has been saved to file.")
else:
print(result.get("error"))
print(result.get("error_description"))
print(result.get("correlation_id"))