[英]Power BI Activities Log REST API Get Access Token
I have been going crazy trying to pull activity logs from the Power BI Rest API.我一直在疯狂地试图从 Power BI Rest API 中提取活动日志。 I can do it using this "try it" method from Microsoft https://docs.microsoft.com/en-us/rest/api/power-bi/admin/getactivityevents
我可以使用 Microsoft https://docs.microsoft.com/en-us/rest/api/power-bi/admin/getactivityevents中的“试用”方法来做到这一点
I have to login to use this tool and then I get a bearer JWT token that is valid for 1 hour, and can be used in the header of subsequent get requests inside scripts.我必须登录才能使用此工具,然后我得到一个持有者 JWT 令牌,有效期为 1 小时,可用于脚本内后续获取请求的 header 中。 However the token expires after 1 hour, and I have to relogin to get a new token and update my script.
但是令牌会在 1 小时后过期,我必须重新登录才能获取新令牌并更新我的脚本。 I'm trying to retrieve a JWT token every time it expires without having to login via a GUI.
我试图在每次到期时检索 JWT 令牌,而无需通过 GUI 登录。 This is to basically automate getting logs from Power BI to send to a security application for analysis.
这是为了自动从 Power BI 获取日志以发送到安全应用程序进行分析。 On the Azure/Power BI side, I have registered a Native app using this link https://dev.powerbi.com/Apps .
在 Azure/Power BI 方面,我使用此链接https://dev.powerbi.com/Apps注册了一个本机应用程序。 On the Azure app registrations I have given the app all API permissions.
在 Azure 应用程序注册上,我已授予该应用程序所有 API 权限。
I have some sample python code using the adal library that I'm trying to get working here.Powershell solutions would also be acceptable I guess, but I prefer using python.我有一些示例 python 代码使用我试图在这里工作的 adal 库。我想 Powershell 解决方案也可以接受,但我更喜欢使用 python。
import adal
import requests
import json
import pandas
authority_url = 'https://login.windows.net/common'
resource_url = 'https://analysis.windows.net/powerbi/api'
client_id = 'my client ID'
client_secret = 'my client secret'
context = adal.AuthenticationContext(authority=authority_url,
validate_authority=True,
api_version=None)
# get JWT access token without having to log in
token = context.acquire_token_with_client_credentials(resource_url, client_id, client_secret)
access_token = token.get('accessToken')
logs_request_url = "https://api.powerbi.com/v1.0/myorg/admin/activityevents?startDateTime=%272021-02-20T00%3A00%3A00.000Z%27&endDateTime=%272021-02-20T23%3A59%3A59.000Z%27"
header = {'Authorization': f'Bearer {access_token}','Content-Type': 'application/json'}
response = requests.get(url=logs_request_url, headers=header).content
logs_request = json.loads(response)
# Power BI Activity Logs
print(logs_request)
This code gets me an empty response.这段代码给了我一个空的响应。 If I paste the access_token generated when I logged in using the "try it" tool, it works.
如果我粘贴使用“试用”工具登录时生成的 access_token,它就可以工作。 I'm trying to avoid manually logging in if possible, because this script would run as a scheduled task.
如果可能,我会尽量避免手动登录,因为此脚本将作为计划任务运行。 I would appreciate any assistance on this, as I'm going crazy trying to get this to work.
我会很感激这方面的任何帮助,因为我正疯狂地试图让它发挥作用。
I see that you have granted the necessary permission for the app.我看到您已授予该应用程序的必要权限。
You would need to enable the below setting ( Allow service principals to use read-only Power BI admin APIs ) at the Power Bi tenant level before using the API:在使用 API 之前,您需要在 Power Bi 租户级别启用以下设置(允许服务主体使用只读 Power BI 管理 API ):
Access the tenant settings.访问租户设置。
Under Admin API settings, you will see Allow service principals to use read-only Power BI admin APIs.在管理员 API 设置下,您将看到允许服务主体使用只读 Power BI 管理员 API。
You can refer to this article for a detailed instructions.您可以参考这篇文章以获得详细说明。 : https://docs.microsoft.com/en-us/power-bi/admin/read-only-apis-service-principal-authentication
: https://docs.microsoft.com/en-us/power-bi/admin/read-only-apis-service-principal-authentication
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.