Office 365 Authentication in Power BI

Highlighted
New Contributor

Hi,

My application (ASP.NET MVC) uses office 365 authentication and it working fine. But Now I want to show some power BI reports inside my application. But if I use office 365 access token to access the Power BI web api it is getting failed. Can some one give me the working code for this?

o Generate the Token I have used following Code.

private static string getAccessToken() { var authority = "https://login.windows.net/<tenantId>"; var authContext = new AuthenticationContext(authority); var clientCredential = new ClientCredential("clientId", "client Secret"); var result = authContext.AcquireToken("https://analysis.windows.net/powerbi/api", clientCredential); return result.AccessToken; }

This will return the access token.Now I will use this access token to retrieve the report details from Power BI. 

protected void getReportsButton_Click(object sender, EventArgs e) { var token = getAccessToken(); string responseContent = string.Empty; //Configure datasets request System.Net.WebRequest request = System.Net.WebRequest.Create(String.Format("{0}reports", baseUri)) as System.Net.HttpWebRequest; request.Method = "GET"; request.ContentLength = 0; request.Headers.Add("Authorization", String.Format("Bearer {0}", token)); //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 PBIReports PBIReports = JsonConvert.DeserializeObject<PBIReports>(responseContent); tb_reportsResult.Text = string.Empty; //Get each Dataset from foreach (PBIReport rpt in PBIReports.value) { tb_reportsResult.Text += String.Format("{0}\t{1}\t{2}\n", rpt.id, rpt.name, rpt.embedUrl); } } } }      

 

But this method gives me an error (The remote server returned an error: (403) Forbidden.).

Thank you

Honey Asin

0 Replies