in util/generate_user_token.py [0:0]
def get_user_token(client_id, client_secret, tenant_id):
authority = f"https://login.microsoftonline.com/{tenant_id}"
scopes = ["https://analysis.windows.net/powerbi/api/Dataset.Read.All"]
redirect_uri = "http://localhost:8000"
app = msal.ConfidentialClientApplication(client_id, authority=authority, client_credential=client_secret)
auth_flow = app.initiate_auth_code_flow(scopes, redirect_uri=redirect_uri)
print(f"{GREEN_BOLD}Please navigate to the following URL to sign in:{RESET_COLOR}")
print(auth_flow["auth_uri"])
result_container = {}
server = start_local_server(8000, result_container)
timeout = 120
start_time = time.time()
while "query_params" not in result_container and (time.time() - start_time) < timeout:
time.sleep(1)
server.shutdown()
if "query_params" not in result_container:
raise Exception("Timeout waiting for the authorization code.")
result = app.acquire_token_by_auth_code_flow(auth_flow, result_container["query_params"])
if "access_token" in result:
return result["access_token"]
else:
raise Exception("Failed to obtain access token: " + str(result.get("error_description")))