Quantcast
Channel: Developer topics
Viewing all articles
Browse latest Browse all 17908

403 Forbidden error occured when using REST API

$
0
0

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.

 

 


Viewing all articles
Browse latest Browse all 17908

Trending Articles