简体   繁体   English

使用Python验证Power BI REST API时出现问题

[英]Problem authenticating to Power BI REST API with Python

I've created a push streaming dataset (history on) and I've managed to post data to it from a Python script using the "Push URL" which I got from the API Info tab for the dataset in question. 我已经创建了一个推送流数据集(历史记录),并且已经设法使用“ Push URL”从Python脚本向其发布数据,该URL是从有关数据集的“ API信息”选项卡中获得的。 What I also need to do is to delete the historic data so as to clear out my test data and/or be able to reset the dataset and re-populate from scratch as and when necessary. 我还需要做的就是删除历史数据,以便清除我的测试数据和/或能够重置数据集,并在必要时从头开始重新填充。

The Push Url is of the form https://api.powerbi.com/beta/xxxxxxxx/datasets/xxxxxxxxxxxx/rows?key=xxxxxxxxxxxxxxx 推送网址的格式为https://api.powerbi.com/beta/xxxxxxxx/datasets/xxxxxxxxxxxx/rows?key=xxxxxxxxxxxxxxx

The following code works fine and the data is posted; 以下代码可以正常工作并发布数据;

import requests 
import pyodbc as db
import pandas as pd

API_ENDPOINT = "https://api.powerbi.com/beta/xxxxxxxx/datasets/xxxxxxxxxxxx/rows?key=xxxxxxxxxxxxxxx"

dbcon = db.connect('DRIVER={SQL Server};SERVER=tcp:fxdb.database.windows.net;DATABASE=FXDatabase;UID=xxxx;PWD=xxxx')
df = pd.read_sql("select statement etc...", dbcon)
data = df.to_dict(orient='records')

response = requests.post(API_ENDPOINT, json=data)

But adding this: 但是添加以下内容:

response = requests.delete(API_ENDPOINT)

gives me: 给我:

404

{
  "error":{
    "code":"","message":"No HTTP resource was found that matches the request URI 'http://api.powerbi.com/beta/...

I couldn't figure this out so I started looking into OAuth2 authentication thinking that perhaps the Auth URL is only for posting data. 我无法弄清楚,因此我开始研究OAuth2身份验证,认为Auth URL 用于发布数据。 After registering the app at https://dev.powerbi.com/apps my code now looks like this: https://dev.powerbi.com/apps注册应用程序后,我的代码现在如下所示:

import requests 
import pyodbc as db
import pandas as pd

API_ENDPOINT = "https://api.powerbi.com/beta/xxxxxxxxxxxxxx/datasets/xxxxxxxxxxxxxxx/rows"

data = {
        'grant_type': 'password',
        'scope': 'openid',
        'resource': r'https://analysis.windows.net/powerbi/api',
        'client_id': 'xxxxxxxxx',
        'username': 'xxxxxxxxx',
        'password': 'xxxxxxxx'
    }
response = requests.post('https://login.microsoftonline.com/common/oauth2/token', data=data)

access_token = response.json().get('access_token')
headers = {'Authorization': 'Bearer ' + access_token}

dbcon = db.connect('DRIVER={SQL Server};SERVER=tcp:fxdb.database.windows.net;DATABASE=FXDatabase;UID=xxxx;PWD=xxxx')
df = pd.read_sql("select statement etc...", dbcon)
data = df.to_dict(orient='records')

response = requests.post(API_ENDPOINT, json=data, headers=headers)

response = requests.delete(API_ENDPOINT, headers=headers)

The authentication works, returning status code 200. The POST returns 401 (this worked with the previous method) and the DELETE still returns 404. 身份验证有效,返回状态码200。POST返回401(这与以前的方法一起使用),而DELETE仍返回404。

Thanks to jonrsharpe who pointed me in the right direction. 感谢jonrsharpe向我指出了正确的方向。

Revisiting the API documentation I discovered a call to get the table names; 重新访问API文档时,我发现了一个获取表名称的调用。

GET https://api.powerbi.com/v1.0/myorg/datasets/ {datasetKey}/tables GET https://api.powerbi.com/v1.0/myorg/datasets/ {datasetKey} / tables

so after authenticating I ran; 所以在验证之后我就跑了;

response = requests.get("https://api.powerbi.com/v1.0/myorg/datasets/xxxxxxxx/tables", headers=headers)

The content of the response told me that there was a table called "RealTimeData" inside my dataset, must be a default name because I haven't knowingly created this table. 响应的内容告诉我,我的数据集中有一个名为“ RealTimeData”的表,该表必须为默认名称,因为我没有有意创建此表。

I have now updated the endpoint to; 我现在将端点更新为;

API_ENDPOINT = "https://api.powerbi.com/v1.0/myorg/datasets/xxxxxxxxx/tables/RealTimeData/rows"

and all works perfectly. 而且一切正常。

Thanks Jon! 谢谢乔恩!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM