connecting to Power BI API using non-interactive authentication

0 votes

Coding in C#. I'm following this guide:

https://azure.microsoft.com/en-us/documentation/articles/resource-group-authenticate-service-principal/#authenticate-service-principal-with-password---powershell%E2%80%8C%E2%80%8B

But it is not working and it is not Power BI specific so I'm not sure exactly how to apply it to the Power BI API.

In my attempt to connect to Power BI I am getting a 403 Forbidden response.

        var authenticationContext = new AuthenticationContext("https://login.windows.net/" + Properties.Settings.Default.TenantID);
        var credential = new ClientCredential(clientId: Properties.Settings.Default.ClientID, clientSecret: Properties.Settings.Default.ClientSecretKey);
        var result = authenticationContext.AcquireToken(resource: "https://management.core.windows.net/", clientCredential: credential);

        if (result == null)
        {
            throw new InvalidOperationException("Failed to obtain the JWT token");
        }

        string accessToken = result.AccessToken;


        string responseContent = string.Empty;

        //The resource Uri to the Power BI REST API resource
        string datasetsUri = "https://api.powerbi.com/v1.0/myorg/datasets";

        //Configure datasets request
        System.Net.WebRequest request = System.Net.WebRequest.Create(datasetsUri) as System.Net.HttpWebRequest;
        request.Timeout = 20000;
        request.Method = "GET";
        request.ContentLength = 0;
        request.Headers.Add("Authorization", String.Format("Bearer {0}", accessToken));

        try
        {

            //Get datasets response from request.GetResponse()
            using (var response = request.GetResponse() as System.Net.HttpWebResponse)
            {
                //Get reader from response stream
                using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
                {
                    responseContent = reader.ReadToEnd();

                    //Deserialize JSON string
                    //JavaScriptSerializer class is in System.Web.Script.Serialization
                    JavaScriptSerializer json = new JavaScriptSerializer();
                    Datasets datasets = (Datasets)json.Deserialize(responseContent, typeof(Datasets));

                    resultsTextbox.Text = string.Empty;
                    //Get each Dataset from 
                    foreach (dataset ds in datasets.value)
                    {
                        resultsTextbox.Text += String.Format("{0}\t{1}\n", ds.Id, ds.Name);
                    }
                }
            }
        }
        catch (WebException wex)
        {
            resultsTextbox.Text = wex.Message;
        }
    }

Oct 30, 2018 in Power BI by Upasana
• 8,620 points
2,013 views

1 answer to this question.

0 votes

Try changing the resource URI:

var result = authenticationContext.AcquireToken(resource: "https://management.core.windows.net/", clientCredential: credential);

to

var result = authenticationContext.AcquireToken(resource: **"https://analysis.windows.net/powerbi/api"**, clientCredential: credential);

You want to acquire a token for the power bi api.

Hope, it helps.

answered Oct 30, 2018 by Shubham
• 13,490 points

Related Questions In Power BI

0 votes
3 answers

How do I get token using javascript API while trying to embed graphs using Power BI

Use ADAL.js that will give you an ...READ MORE

answered Jun 22, 2019 in Power BI by anonymous
4,198 views
+1 vote
1 answer

Is it possible to manage limits while using dedicated capacity power bi embedded app service?

These limits are not related to using ...READ MORE

answered Sep 24, 2018 in Power BI by Hannah
• 18,570 points
548 views
+2 votes
2 answers

Power BI service throwing 403 error when using token to embed Power BI report

To add to @Kalgi's answer, Also make ...READ MORE

answered Oct 3, 2018 in Power BI by Nilesh
• 7,050 points
5,479 views
0 votes
1 answer

Error:connecting to Azure SSAS server in Power-BI through Import mode

If I understand correctly, you're connecting to ...READ MORE

answered Nov 9, 2018 in Power BI by Upasana
• 8,620 points
2,424 views
0 votes
1 answer

Displaying Table Schema using Power BI with Azure IoT Hub

Answering your first question, Event Hubs are ...READ MORE

answered Aug 1, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
1,022 views
+1 vote
1 answer

Unable to install connector for Power Bi and PostgreSQL

I think the problem is not at ...READ MORE

answered Aug 22, 2018 in Power BI by nirvana
• 3,130 points
2,484 views
0 votes
1 answer

How to assign Power BI license to a new user

The assign license method of MS Graph API might ...READ MORE

answered Sep 25, 2018 in Power BI by Kalgi
• 52,360 points
679 views
0 votes
1 answer

Where is Power Bi being hosted?

Power BI is currently cloud hosted by ...READ MORE

answered Sep 28, 2018 in Power BI by Kalgi
• 52,360 points
861 views
0 votes
1 answer

Calling Azure API using Power BI with Next Link

Try this let iterations = ...READ MORE

answered Apr 9, 2019 in Power BI by Shubham
• 13,490 points
1,609 views
0 votes
1 answer

Connecting POWER BI web with AWS mysql database

So, there's no direct way to connect ...READ MORE

answered Nov 9, 2018 in Power BI by Shubham
• 13,490 points
2,970 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP