首页 > 解决方案 > Powerbi api 的 POST 方法失败但 GET 正在工作(Python)

问题描述

我正在尝试使用 python 刷新数据流。它在 Powershell 中为我工作。我不确定我是否正确使用了 Python。使用 Python,我可以让方法正常工作。

Python(获取方法:-> 工作)

import requests
groupID = <groupid>
#url = 'https://api.powerbi.com/v1.0/myorg/capacities'
header = {'Authorization': f'Bearer {access_token}','Content-Type':'application/json'}
datasets_request_url = 'https://api.powerbi.com/v1.0/myorg/groups/' + groupID + '/datasets'
response = requests.get(url=datasets_request_url, headers=header)

Python 失败(POST 方法)

dataflowid = <dataflowid>
dataflowrefresh_url = 'https://api.powerbi.com/v1.0/myorg/groups/' + groupID + '/dataflows/'+dataflowid+'/refreshes'
response = requests.post(url=dataflowrefresh_url, headers=header)

HTTPError:400 客户端错误:对 url 的错误请求:

PowerShell(POST 方法工作)

$workspaceid = <groupid>
$dataflowid = <dataflowid>
$uri = "https://api.powerbi.com/v1.0/myorg/groups/$workspaceid/Dataflows/$dataflowid/refreshes"

# Build JSON, convert back and forth as we're just defining it as a string.
$json = '{"notifyOption": "MailOnFailure"}' | ConvertFrom-Json
$body = $json | ConvertTo-Json

# Refresh the dataflow
Invoke-RestMethod -Uri $uri -Headers $authHeader -body $body -Method POST -Verbose

如果我删除参数 $body,它会失败并显示错误 400。我不知道如何将工作的 Powershell api 调用转换为 Python。

使用以下代码生成的相同令牌。POST 方法在 Powershell 中有效,但在 Python 中无效,GET 在两者中都有效。

context = adal.AuthenticationContext(authority=authority_url,

                                         validate_authority=True,
                                         api_version=None)
    
token = context.acquire_token_with_client_credentials(resource_url, client_id, client_secret)
access_token = token.get('accessToken')

标签: pythonpowershellapipowerbi

解决方案


url = 'https://api.powerbi.com/v1.0/myorg/groups/' + workspaceID + '/dataflows/' +dataflowid+ '/refreshes'
payload= {'refreshRequest': 'y'}
data = json.dumps(payload)
headers = header
response = requests.request("POST", url, headers=headers, data=data)

使用 refreshRequest : 'Y' 使它工作。在powershell中,我不必给这个参数。


推荐阅读