-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.py
72 lines (53 loc) · 3.08 KB
/
example.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
"""
Simple example code to communicate with Power BI REST API.
(c) Emmanuel Prochasson, Omnistream 2018
Licence: MIT
"""
import requests
# Configuration goes here:
RESOURCE = "https://analysis.windows.net/powerbi/api" # Don't change that.
APPLICATION_ID = "abcdef-abcdef-abcdef-abcdef" # The ID of the application in Active Directory
APPLICATION_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxx" # A valid key for that application in Active Directory
USER_ID = "emmanuel@your_company.com" # A user that has access to PowerBI and the application
USER_PASSWORD = "password" # The password for that user
GROUP_ID = 'xxxxxxxxxxx' # The id of the workspace containing the report you want to embed
REPORT_ID = 'xxxxxxxxxxxxxx' # The id of the report you want to embed
def get_access_token(application_id, application_secret, user_id, user_password):
data = {
'grant_type': 'password',
'scope': 'openid',
'resource': "https://analysis.windows.net/powerbi/api",
'client_id': application_id,
'client_secret': application_secret,
'username': user_id,
'password': user_password
}
token = requests.post("https://login.microsoftonline.com/common/oauth2/token", data=data)
assert token.status_code == 200, "Fail to retrieve token: {}".format(token.text)
print("Got access token: ")
print(token.json())
return token.json()['access_token']
def make_headers(application_id, application_secret, user_id, user_password):
return {
'Content-Type': 'application/json; charset=utf-8',
'Authorization': "Bearer {}".format(get_access_token(application_id, application_secret, user_id, user_password))
}
def get_embed_token_report(application_id, application_secret, user_id, user_password, group_id, report_id):
endpoint = "https://api.powerbi.com/v1.0/myorg/groups/{}/reports/{}/GenerateToken".format(group_id, report_id)
headers = make_headers(application_id, application_secret, user_id, user_password)
res = requests.post(endpoint, headers=headers, json={"accessLevel": "View"})
return res.json()['token']
def get_groups(application_id, application_secret, user_id, user_password):
endpoint = "https://api.powerbi.com/v1.0/myorg/groups"
headers = make_headers(application_id, application_secret, user_id, user_password)
return requests.get(endpoint, headers=headers).json()
def get_dashboards(application_id, application_secret, user_id, user_password, group_id):
endpoint = "https://api.powerbi.com/v1.0/myorg/groups/{}/dashboards".format(group_id)
headers = make_headers(application_id, application_secret, user_id, user_password)
return requests.get(endpoint, headers=headers).json()
def get_reports(application_id, application_secret, user_id, user_password, group_id):
endpoint = "https://api.powerbi.com/v1.0/myorg/groups/{}/reports".format(group_id)
headers = make_headers(application_id, application_secret, user_id, user_password)
return requests.get(endpoint, headers=headers).json()
# ex:
# get_embed_token_report(APPLICATION_ID, APPLICATION_SECRET, USER_ID, USER_PASSWORD, GROUP_ID, REPORT_ID)