我的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