Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
Hello,
We are using an angular2 SPA to interact with our REST API services. We have a report service that we would like to use to call the Power BI REST API service endpoints. We acquire an azure AD accessToken in our service and try and use that token for the PowerBI calls but keep getting the "The remote server returned an error: (403) Forbidden." error. Our SPA is registered in Azure and we have given Power BI permissions to the application. I have an account in Power BI as well. Below is the code I am using to call the GetReports endpoint. The error is thrown when the GetResponse is called. Any suggestions would be greatly appeciated. Thanks.
var accessToken = Request.Headers.Authorization.Parameter; //defined in controller and passed to method below.
Uri baseAddress = new Uri("https://api.powerbi.com/"); //defined above
public async Task<List<PbiReport>> GetPBIReports(string accessToken)
{
string responseData;
var powerBiApiUrl = baseAddress + "v1.0/myorg/datasets";
List<PbiReport> reports = new List<PbiReport>();
HttpWebRequest request = WebRequest.Create(powerBiApiUrl) as HttpWebRequest;
request.KeepAlive = true;
request.Method = "GET";
request.ContentLength = 0;
request.ContentType = "application/json";
request.Headers.Add("Authorization", $"Bearer {accessToken}");
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
reports = JsonConvert.DeserializeObject<List<PbiReport>>(reader.ReadToEnd());
}
}
return reports;
}
The code looks good.
Ensure the accessToken is valid. You can try to access the API with POSTMAN with the accesstoken.
For testing purpose, you can also get a valid token with the help of Chrome dev tool(F12).
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
14 | |
2 | |
2 | |
1 | |
1 |