python - 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')
解决方案
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中,我不必给这个参数。
推荐阅读
- c# - 如何在数组中选择多个值?
- c# - C# 中的“as”关键字是否仅将 Object 类型变量转换为我想要的类型?
- vb.net - 如何在不悬停鼠标的情况下在用户窗体上显示按钮控件,如弹出模式中的按钮
- html - 在 IE11 中选择选项后下拉菜单向上移动
- boost - 如何从使用类型隐藏的索引形成的多索引容器中按名称获取索引
- java - 在 sbt 中添加类路径位置
- rundeck - 安装 EC2-steps-plugin 时遇到问题
- javascript - 在 JSP 中看不到 Json 数据结果
- c++ - 从 .tsv 文件读入
- node.js - 从 Angular 和 Nodejs 请求 API 时出错