gpt4 book ai didi

api - 使用 Python 访问 Power BI API

转载 作者:行者123 更新时间:2023-12-03 03:06:02 24 4
gpt4 key购买 nike

我的要求是使用 Python 将实时数据推送到 Power BI,首先从数据库中读取数据,然后将数据发送到 Power BI 中的流数据集中。

我想要的第一件事是对 Power BI 进行简单的“get”调用。

official documentation解释了通过客户端应用程序或 Web 应用程序的 REST API 连接到 Power BI 的过程。但是,我正在使用 Python - 不确定它是客户端应用程序还是 Web 应用程序。

无论如何,我可以使用 adal 库和方法 .acquire_token_with_client_credentials 获取 accessToken ,它要求authority_uri、tenant、client_id和client_secret(注意这不是要求用户名和密码)。顺便说一句,我还尝试使用 .acquire_token_with_username_password 获取 accessToken ,但这没有用。

不幸的是,当我将以下代码与获得的 accessToken 一起使用时,我收到响应 403。

#accessToken is received using the adal libary 
headers = {'Authorization': 'Bearer ' + accessToken, 'Content-Type': 'application/json'}
read_datasets = requests.get('https://api.powerbi.com/v1.0/myorg/datasets', headers=headers)
#shockingly, this will result in a response 403

阅读其他 stackoverflow 帖子并查看控制台应用程序后,我认为这不起作用的原因是没有用户登录过程。

This thread提到仅使用客户端凭据是不够的(获取 accessToken 就足够了,但使用 API 还不够)

不知道如何继续,但我需要的也许是一种继续使用这个 adal template 的方法这为我提供了 accessToken,并提供我的用户名和密码(如果需要),并与 accessToken 一起访问 API。

最佳答案

我看到您已在 PowerBI 论坛上回答了这个问题:

https://community.powerbi.com/t5/Developer/Access-Power-BI-API-with-Python/m-p/190087#M6029

供将来访问此内容的人引用:

使用 python adal 库和适当的方法获取您的 token 。获得 token 后,您可以将其作为请求 header 的一部分传递,如下所示:

    url = f'{self.api_url}/v1.0/myorg/groups/{self.group_id}/datasets'
headers = {
'Authorization': f'Bearer {self.token["accessToken"]}'
}

其中 api_url 是 https://api.powerbi.com ,group_id 是您的 group_id,token 是您从 acquire_token_with_username_password 获取的 token 字典。

从那里您将能够进行所需的所有 PowerBI API 调用。

关于api - 使用 Python 访问 Power BI API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44394174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com