社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

使用MSAL和Python连接到Power BI REST API

Bill • 2 年前 • 967 次点击  

我的Power BI应用程序已在Azure Active Directory中注册,并具有选定的API权限(如User.Read)。我已尽最大努力遵循MSAL文档,同时搜索此网站和其他网站,并尝试找到的一些代码片段,但我运气不佳。我无法使用公共客户端应用程序生成令牌,我用一个保密的令牌做了进一步的处理——我生成了一个令牌并将请求发送到网站,但由于不确定具体错误是什么,我被困在这里。我也不确定我能显示多少请求文本,因为我不确定什么是保密的,什么不是。

根据我上面提到的研究,我花了几个小时试图通过多种方式使用MSAL连接PBI REST API,并认为是时候寻求帮助了。提前谢谢!

这是我的代码,删除了特定的ID:

#Import msal and requests
import msal
import requests

#Multiple parameters that will be needed
client_id = 'client id code'
client_credential = 'client secret code'
authority = 'https://login.microsoftonline.com/tenant id code'
redirect_uri = 'https://login.microsoftonline.com/common/oauth2/nativeclient'
power_bi_api = 'https://analysis.windows.net/powerbi/api/'
power_bi_api_root = 'https://api.powerbi.com/'
scopes_list = [
    power_bi_api + 'User.Read',
    power_bi_api + 'Dashboard.Read.All',
    power_bi_api + 'Dataset.Read.All',
    power_bi_api + 'Gateway.Read.All',
    power_bi_api + 'Workspace.Read.All'
]
endpoint = 'https://login.microsoftonline.com/tenant id code/oauth2/v2.0/authorize'

#Create a confidential client application
app = msal.ConfidentialClientApplication(
    client_id = client_id,
    client_credential = client_credential,
    authority = authority
)

#Generate a token
token_gen = app.acquire_token_for_client(
    scopes = 'https://analysis.windows.net/powerbi/api/.default'
)
#Returns token_type = Bearer, and gives an access_token
#I'm not sure why I need to use .default here instead of scopes_list,
#  but it didn't work otherwise

#Here is where I'm stuck
header = {'Authorization': 'Bearer ' + token_gen['access_token']}
api_out = requests.get(endpoint, headers = header)
#Returns status code 200
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/159839
 
967 次点击  
文章 [ 1 ]  |  最新文章 2 年前