简体   繁体   English

Power BI 活动日志 REST API 获取访问令牌

[英]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 ):

  • Log into the Power BI admin portal.登录到 Power BI 管理门户。

在此处输入图像描述

  • 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。

在此处输入图像描述

  • Enable the above setting.启用上述设置。

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.

相关问题 使用 Python 检索 Power BI 的 Rest API 的访问令牌? - Using Python to retrieve access token for Power BI's Rest API? API Power BI获取令牌,但获取请求获取响应401 - API Power BI Get token but get request get response 401 使用Python验证Power BI REST API时出现问题 - Problem authenticating to Power BI REST API with Python 使用 Power BI 重命名 Power BI 数据流 REST API - 401 未经授权 - Rename Power BI dataflow using Power BI REST API - 401 Unauthorised 如何解决 Azure ADFS 设置中的 Power BI API 令牌问题? - How to resolve the Power BI API token issue in the Azure ADFS Setup? 在没有 client_id 的 Python 中获取 Power BI 服务访问令牌 - Acquire Power BI Service Access token in Python without client_id 什么是基于令牌的身份验证,如何使用它访问 REST API GET 请求? NETSUITE API - What is Token Based Authentication, How to access REST API GET request using it? NETSUITE API 如何在OpenCorporates REST API中使用访问令牌? - How to use an access token with the OpenCorporates REST API? Power BI Rest API - AADSTS50034:XXXXX 目录中不存在用户帐户 {EmailHidden} - Power BI Rest API - AADSTS50034: The user account {EmailHidden} does not exist in the XXXXX directory Reddit Api 获取访问令牌 - Reddit Api Get Access Token
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM