ADAL Python刷新PowerBI数据集 [英] ADAL Python to Refresh PowerBI dataset

查看:404
本文介绍了ADAL Python刷新PowerBI数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我正在使用的代码段是:

 导入adal 
从msrestazure.azure_active_directory导入
导入AADTokenCredentials

def authenticate_client_key():

Authority_host_uri ='https://login.microsoftonline.com'
租户='租户'
Authority_uri = Authority_host_uri +'/'+租户
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)
凭据= AADTokenCredentials(mgmt_token,client_id)

返回凭据

来源:> https://azure.microsoft.com/zh-CN/resources/samples/data-lake-analytics-python-auth-options/



根据PowerShell上编写的代码,目的是将access_token插入到以下POST请求的标头中



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



来源: https://powerbi.microsoft.com / en-us / blog /宣布数据更新apis-in-the-power-bi-service /



我尝试使用凭据进入POST请求,但似乎不起作用。



我已经尝试过

  url ='https://api.powerbi.com/v1.0/myorg/groups/me/datasets/datasetid/刷新'
个请求。post(url,data = mgmt_token)

是否可以合并这两个代码?



问候

解决方案

您可以使用pypowerbi软件包可刷新Power BI数据集,或者您可以通过检查代码自行检查如何做。 https://github.com/cmberryau/pypowerbi

  pip install pypowerbi 



< pre class = lang-py prettyprint-override> 从pypowerbi.client导入adal
import PowerBIClient

#您可能需要更改它们,但我对此表示怀疑
Authority_url ='https://login.windows.net/common'
resource_url ='https://analysis.windows.net/powerbi/api'
api_url ='https:/ /api.powerbi.com'

#将这些更改为您的凭据
client_id ='00000000-0000-0000-0000-000000000000'
用户名='someone@somecompany.com '
password ='averygoodpassword'

#首先,您需要使用adal
context进行身份验证= adal.AuthenticationContext(authority = authority_url,
validate_authority = True,
api_version = None)

#获取您的身份验证令牌
令牌= context.acquire_token_with_username_password(资源=资源_url,
client_id = client_id,
用户名=用户名,
密码=密码)

#创建您的powerbi api客户端
客户端= PowerBIClient(api_url,令牌)

#刷新所需的数据集(数据集和组ID可以从浏览器URL中获取)
client.datasets.refresh_dataset(dataset_id ='data-set-id-goes-here',
notify_option ='MailOnCompletion',
group_id ='group-id-goes-here')


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.

The piece of code that I'm using is:

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

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

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

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

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

I have tried

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?

Regards,

解决方案

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')

这篇关于ADAL Python刷新PowerBI数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆