无法使用RestApi获取Power Bi中的数据集列表?

时间:2018-10-20 18:32:04

标签: powerbi

enter image description here获取访问令牌后,通过将GET请求发送给

https://app.powerbi.com/groups/me/datasets/

通过在标头中添加访问令牌

内容类型:application / json 授权:访问令牌

我没有获取数据集,而是在下面获取html内容:

> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" >
>     <head>
>         <title>Power BI</title>
>         <meta http-equiv="X-UA-Compatible" content="IE=edge;" />
> 
>         <meta charset="utf-8">
>         <meta name="description" content="">
>         <meta name="format-detection" content="telephone=no" />
>         <link rel="shortcut icon" href="/images/PowerBI_Favicon.ico" />
>         
>     <meta name="apple-itunes-app" content="app-id=929738808">
>     <meta name="apple-itunes-app-tab" content="app-id=929738808">
>     <meta name="google-play-app" content="app-id=com.microsoft.powerbim">
>     <meta name="google-play-app-tab" content="app-id=com.microsoft.powerbim">
>     <meta http-equiv="x-dns-prefetch-control" content="on"> .............

如何获取json格式的数据集列表?

代码(java sdk):

    String workspacename = "rightws";
    String username = "xxxxxxxxxxx.onmicrosoft.com";
    String workspaceId = "xxxxxxxxxxx1-9812b695603a";
    String reportId="6xxxxxxxxxx4098-a55b-96f8c103edab";
    String accessKey = "xxxxx34d7f7xxxxxxxxxxxx;
    PowerBIToken token = PowerBIToken.CreateReportEmbedToken(
            workspacename,
            workspaceId,
            reportId,
            username,
            null);
    String jwt = token.Generate(accessKey);

System.out.println(jwt);

2 个答案:

答案 0 :(得分:0)

似乎您使用的端点错误。 Rest API的文档在这里:https://docs.microsoft.com/en-us/rest/api/power-bi/datasets

要获取数据集列表,您要么需要使用此端点(请注意,URL中没有“组”名称)

GET https://api.powerbi.com/v1.0/myorg/datasets

或者这一个(这是特定组的数据集-需要组ID)

GET https://api.powerbi.com/v1.0/myorg/groups/{groupId}/datasets

您似乎正在使用返回组的端点。我认为“ / me / datasets”必须被忽略

GET https://api.powerbi.com/v1.0/myorg/groups

希望这会有所帮助

答案 1 :(得分:0)

尝试使用此代码:

        var request = WebRequest.Create("https://api.powerbi.com/v1.0/myorg/datasets") as HttpWebRequest;
        request.KeepAlive = true;
        request.Method = "GET";
        request.ContentLength = 0;
        request.ContentType = "application/json";
        request.Headers.Add("Authorization", $"Bearer {accessToken}");
        using (HttpWebResponse httpResponse = request.GetResponse() as System.Net.HttpWebResponse)
        {
            using (StreamReader reader = new System.IO.StreamReader(httpResponse.GetResponseStream()))
            {
                string responseContent = reader.ReadToEnd();
                MessageBox.Show(responseContent, "Get Datasets");
            }
        }

如果仍然出现403错误,请确保在注册应用程序时已授予读取数据集的权限。尝试对https://jwt.io处的访问令牌进行解码,看看它是否包含Dataset.Read.All in scp:

enter image description here

更新:看来您的令牌没有授予您获取数据集列表的权利。尝试注册一个新的本机应用程序,并确保选中“数据集API”中的“读取所有数据集”复选框。然后尝试使用以下代码获取访问令牌:

string redirectUri = "https://login.live.com/oauth20_desktop.srf";
string resourceUri = "https://analysis.windows.net/powerbi/api";
string authorityUri = "https://login.windows.net/common/oauth2/authorize";
string clientId = "xxxxxx";
AuthenticationContext authContext = new AuthenticationContext(authorityUri, new TokenCache());
var authenticationResult = await authContext.AcquireTokenAsync(resourceUri, clientId, new Uri(redirectUri), new PlatformParameters(PromptBehavior.Auto));
if (authenticationResult != null)
    GetListOfDatasets(authenticationResult.AccessToken);

更新:要使用Power BI Client库列出数据集,您需要这样的代码。首先,您需要使用AcquireTokenAsync(提供用户名和密码,或以交互方式提示)进行身份验证,然后将此访问令牌传递给客户端并调用GetDatasetsInGroupWithHttpMessageAsync方法。

private static string resourceUri = "https://analysis.windows.net/powerbi/api";
private static string apiUrl = "https://api.powerbi.com";
private static string clientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
private static string groupId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

UserPasswordCredential uc = new UserPasswordCredential("someuser@example.com", "some strong password");
AuthenticationResult authenticationResult = authContext.AcquireTokenAsync(resourceUri, clientId, uc).Result;
TokenCredentials credentials = new TokenCredentials($"{authenticationResult.AccessToken}", "Bearer");
using (var client = new Microsoft.PowerBI.Api.V2.PowerBIClient(new Uri(apiUrl), credentials))
{
    var resultDatasets = await client.Datasets.GetDatasetsInGroupWithHttpMessagesAsync(groupId);
    foreach (var item in resultDatasets.Body.Value)
        MessageBox.Show($"{item.Name} ({item.Id})");
}
相关问题