无法使用 RestApi 获取 power bi 中的数据集列表? [英] Unable to get list of datasets in power bi using RestApi?

查看:14
本文介绍了无法使用 RestApi 获取 power bi 中的数据集列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:您的令牌似乎没有授予您获取数据集列表的权利.尝试注册一个新的本机应用程序,并确保从数据集 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";字符串 clientId = "xxxxxx";AuthenticationContext authContext = new AuthenticationContext(authorityUri, new TokenCache());var authenticationResult = await authContext.AcquireTokenAsync(resourceUri, clientId, new Uri(redirectUri), new PlatformParameters(PromptBehavior.Auto));如果(身份验证结果!= null)GetListOfDatasets(authenticationResult.AccessToken);

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

私有静态字符串 resourceUri = "https://analysis.windows.net/powerbi/api";私有静态字符串 apiUrl = "https://api.powerbi.com";私有静态字符串 clientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";私有静态字符串 groupId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";UserPasswordCredential uc = new UserPasswordCredential("someuser@example.com", "一些强密码");AuthenticationResult authenticationResult = authContext.AcquireTokenAsync(resourceUri, clientId, uc).Result;TokenCredentials 凭证 = new TokenCredentials($"{authenticationResult.AccessToken}", "Bearer");使用 (var client = new Microsoft.PowerBI.Api.V2.PowerBIClient(new Uri(apiUrl), credentials)){var resultDatasets = 等待 client.Datasets.GetDatasetsInGroupWithHttpMessagesAsync(groupId);foreach(resultDatasets.Body.Value 中的变量项)MessageBox.Show($"{item.Name} ({item.Id})");}

enter image description hereAfter getting Access token, by sending GET request to

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

by adding access token in header

content-type: application/json authorization: access token

instead of getting datasets i am getting html content below:

> <!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"> .............

how to get list of datasets in json format?

code(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);

解决方案

Try using this code:

        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");
            }
        }

If you keep getting 403 error, then make sure you granted rights to read datasets when you register your application. Try to decode the access token at https://jwt.io and see does it contains Dataset.Read.All in scp:

UPDATE: It looks like your token doesn't give you rights to get the list of datasets. Try to register a new native application and make sure you select "Read All Datasets" checkbox from Dataset APIs. Then try to obtain the access token with code like this:

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

UPDATE: To list datasets using the Power BI Client library, you need code like this. First you need to authenticate using AcquireTokenAsync (either providing user name and password, or be prompted interactively), then pass this access token to your client and call GetDatasetsInGroupWithHttpMessageAsync method.

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})");
}

这篇关于无法使用 RestApi 获取 power bi 中的数据集列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆