powerbi中python网站数据_ADAL Python刷新PowerBI数据集

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值