ADAL Python 刷新 PowerBI 数据集

2022-01-10 00:00:00 python powerbi

问题描述

我在 Azure 文档中发现了一段代码,它允许在没有 MFA 的情况下获取凭据.但我想知道是否可以使用它连接到 PowerBI API.

I found a piece of code on Azure documentation that allows getting credentials without MFA. But I'm wondering if is possible to use it to connect to PowerBI API.

我正在使用的代码是:

import adal
import requests
from msrestazure.azure_active_directory import AADTokenCredentials

def authenticate_client_key():

    authority_host_uri = 'https://login.microsoftonline.com'
    tenant = 'tenant'
    authority_uri = authority_host_uri + '/' + tenant
    resource_uri = 'https://management.core.windows.net/'
    client_id = 'clientid'
    client_secret = 'client-secret'

    context = adal.AuthenticationContext(authority_uri, api_version=None)
    mgmt_token = context.acquire_token_with_client_credentials(resource_uri, client_id, client_secret)
    credentials = AADTokenCredentials(mgmt_token, client_id)

    return credentials

来源:https://azure.microsoft.com/en-us/resources/samples/data-lake-analytics-python-auth-options/

根据写在PowerShell上的代码,目的是将access_token插入到后面的POST请求的header中

According to the code written on PowerShell, the aim is to insert the access_token into the header of the following POST request

POST https://api.powerbi.com/v1.0/myorg/groups/me/datasets/{dataset_id}/refreshes

POST https://api.powerbi.com/v1.0/myorg/groups/me/datasets/{dataset_id}/refreshes

来源:https://powerbi.microsoft.com/en-us/blog/announcing-data-refresh-apis-in-the-power-bi-service/

我尝试在 POST 请求中使用凭据,但似乎不起作用.

I have tried to use the credentials into the POST request, but seems is not working.

我试过了

url = 'https://api.powerbi.com/v1.0/myorg/groups/me/datasets/datasetid/refreshes'
requests.post(url,data=mgmt_token)

这两个代码可以合并吗?

Is it possible to merge this two codes?

问候,


解决方案

您可以使用 pypowerbi 包刷新 Power BI 数据集,也可以通过检查代码来检查如何自己执行此操作.https://github.com/cmberryau/pypowerbi

You can use the pypowerbi package to refresh Power BI datasets or you can check how to do it yourself by inspecting the code. https://github.com/cmberryau/pypowerbi

pip install pypowerbi

import adal
from pypowerbi.client import PowerBIClient

# you might need to change these, but i doubt it
authority_url = 'https://login.windows.net/common'
resource_url = 'https://analysis.windows.net/powerbi/api'
api_url = 'https://api.powerbi.com'

# change these to your credentials
client_id = '00000000-0000-0000-0000-000000000000'
username = 'someone@somecompany.com'
password = 'averygoodpassword'

# first you need to authenticate using adal
context = adal.AuthenticationContext(authority=authority_url,
                                     validate_authority=True,
                                     api_version=None)

# get your authentication token
token = context.acquire_token_with_username_password(resource=resource_url,
                                                     client_id=client_id,
                                                     username=username,
                                                     password=password)

# create your powerbi api client
client = PowerBIClient(api_url, token)

# Refresh the desired dataset (dataset and group IDs can be taken from the browser URL)
client.datasets.refresh_dataset(dataset_id='data-set-id-goes-here',
                                notify_option='MailOnCompletion',
                                group_id='group-id-goes-here')

相关文章