cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
s8nRrG Frequent Visitor
Frequent Visitor

403 Forbidden error occured when using REST API

I'm developing API that allows to send data directly to Power BI. There must not be any user's interaction to authenticate the request. The user can set only clientId and clientSecret values. But all my requests retrun 403 status code. For instance, let's look at my code that I use to get data about all my datasets:

 

 

private static string GetAccessToken()
{
    const string clientId = "valid client id";
    const string clientSecret = "valid client secret";
    var clientCredentials = new ClientCredential(clientId, clientSecret);
    const string authorityUri = "https://login.windows.net/common/oauth2/authorize";
    const string resourceUri = "https://analysis.windows.net/powerbi/api";
	
    authContext = new AuthenticationContext(authorityUri, new TokenCache());
    var token = authContext.AcquireTokenAsync(resourceUri, clientCredentials).Result.AccessToken;
    return token;
}

private static string GetDatasets()
{
    var powerBIApiUrl = "https://api.powerbi.com/v1.0/myorg/datasets";

    var token = GetAccessToken();

    HttpWebRequest request = WebRequest.Create(powerBIApiUrl) as HttpWebRequest;
    request.Method = "GET";
    request.ContentLength = 0;

    request.Headers.Add("Authorization", $"Bearer {token}");
// 403 The remote server returned an error: (403) Forbidden. using (var httpResponse = request.GetResponse() as HttpWebResponse { using (var reader = new StreamReader(httpResponse.GetResponseStream())) { var responseContent = reader.ReadToEnd(); return responseContent; } } }

 

I get token "eyJ0...cceA" that seems valid. However, request.GetResponse() returns 403 status code. All permissions were delegated to Power BI Service in in Azure Active Directory.

 

Do you have any idea how to fix this? I really appreciate any help.

 

 

19 REPLIES 19

Re: 403 Forbidden error occured when using REST API

Can you post the complete code?

Sunkari Member
Member

Re: 403 Forbidden error occured when using REST API

I hope your registered your app with Azure Active directory  and provided proper clientid,clientkey,username and pwd.

 

https://login.microsoftonline.com/yourdirectorykey/oauth2/token

 

Provide proper directory key to resolve your issues.

 

Let me know if you need any further details

hari_seenivasan Frequent Visitor
Frequent Visitor

Re: 403 Forbidden error occured when using REST API

Try the following API end point

I believe its still in beta mode.

https://api.powerbi.com/beta/myorg/datasets
s8nRrG Frequent Visitor
Frequent Visitor

Re: 403 Forbidden error occured when using REST API


@Sunkari wrote:

I hope your registered your app with Azure Active directory  and provided proper clientid,clientkey,username and pwd.

 

https://login.microsoftonline.com/yourdirectorykey/oauth2/token

 

Provide proper directory key to resolve your issues.

 

Let me know if you need any further details


 

I use correct directory key. If I used wrong directory key, I'd got the exception No service namespace named 'wrong directory key' was found in the data store during obtaining access token.

s8nRrG Frequent Visitor
Frequent Visitor

Re: 403 Forbidden error occured when using REST API


@hari_seenivasan wrote:

Try the following API end point

I believe its still in beta mode.

https://api.powerbi.com/beta/myorg/datasets

It works with the same 403 status code.

hari_seenivasan Frequent Visitor
Frequent Visitor

Re: 403 Forbidden error occured when using REST API

Redirect response to HTML and share here, we will get more details on what went wrong.

kalcey Frequent Visitor
Frequent Visitor

Re: 403 Forbidden error occured when using REST API

Can you please elaborate on where the username/password go?

Thanks

alanhodgson Established Member
Established Member

Re: 403 Forbidden error occured when using REST API

I am having this same problem. I get a valid token, but when I pass the request, I get the HTTP Error 403: Forbidden. In Azure AD, I delegated permissions, and I tested sending request through Apiary and it worked.

 

Anyone have a solution?

 

Thanks,

 

Alan

nlombardiCL Frequent Visitor
Frequent Visitor

Re: 403 Forbidden error occured when using REST API

Anyone find a solution to this?  I'm having the same issue.  We receive the access token in our REST service and I'm passing it to the Power BI REST api and we get this same error.