Unable to get list of datasets in power bi using RestApi?

前端 未结 2 1155
后悔当初
后悔当初 2021-01-27 23:44

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

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

by adding access

相关标签:
2条回答
  • 2021-01-28 00:25

    It appears you may be using the wrong endpoint. The documentation for the Rest API is here: https://docs.microsoft.com/en-us/rest/api/power-bi/datasets

    To get a list of data sets you either need to use this endpoint (note there is no "group" designation in URL)

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

    Or this one (which is data sets for a specific group -- group ID is required)

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

    You appear to be using the endpoint that returns groups. I think "/me/datasets" must be getting ignored

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

    Hope this helps

    0 讨论(0)
  • 2021-01-28 00:34

    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})");
    }
    
    0 讨论(0)
提交回复
热议问题