Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
emotix
Frequent Visitor

Report GenerateTokenForCreateAsync Unauthorized Response

We're trying to embed Power BI reports in 'Create' mode but having a bit of an issue obtaining the Token for this action.

 

According to the MSDN documentation, 'GenerateTokenRequest' we must pass 'create', the data set id, and 'true'. However, when we call 'GenerateTokenForCreateAsync ' (we've tried 'GenerateTokenForCreateInGroupAsync,' too) we get "Operation returned an invalid status code 'Unauthorized'".

 

  • We can confirm the power bi user has full permissions and the Azure permissions are configured as well.
  • This app is a 'Native' app where our App owns the data.
  • We can obtain tokens to view reports without issue, access datasets, groups, etc.

Another user here had a similar issue, however we don't have the Bearer token as we're authenticating a bit differently. We're unsure if we're hitting an unsupported method. There are few results online for this and no examples we can track down beyond the embed samples which just show the JavaScript side of things.

 

Here's what we have:

 

// Create a Power BI Client object. It will be used to call Power BI APIs.
using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials))
            {
                // Get a list of reports.
                var datasets = await client.Datasets.GetDatasetsInGroupAsync(groupId);

                // Get the first report in the group.
                var dataset = datasets.Value.FirstOrDefault(f => f.Id == datasetId);

                if (dataset == null)
                {
                    return new EmbedConfig
                    {
                        ErrorMessage = "Group has no datasets."
                    };
                }

                // Generate Embed Token.
                var generateTokenRequestParameters = new GenerateTokenRequest("create", datasetId, true);
                var tokenResponse = await client.Reports.GenerateTokenForCreateInGroupAsync(groupId, generateTokenRequestParameters);

                if (tokenResponse == null)
                {
                    return new EmbedConfig()
                    {
                        ErrorMessage = "Failed to generate embed token."
                    };
                }

                // Generate Embed Configuration.
                var embedConfig = new EmbedConfig()
                {
                    EmbedToken = tokenResponse,
                    Id = dataset.Id
                };

                return embedConfig;
            }

 

4 REPLIES 4
srashtisj
New Member

We are facing same issue.

 

Please let us know how you resolved it.

Eric_Zhang
Microsoft Employee
Microsoft Employee

@emotix

Is the user an admin in the workspace? To get more specific error message, you can add try..catch block to your code.

 

 

try
                {
                    var generateTokenRequestParameters = new GenerateTokenRequest("create", datasetId, true); 
var tokenResponse = await client.Reports.GenerateTokenForCreateInGroupAsync(groupId, generateTokenRequestParameters); } catch (HttpOperationException ex) { //Bad Request var content = ex.Response.Content; Console.WriteLine(content); }

 

By the way, why "you don't have the Bearer token", what is that authentication? With a Bearer token you can test in POSTMAN before in C#.

 

Yes, the admin is in the workspace and adding a try/catch just showed the error was Unauthorized.

 

I did some more digging and obtain the Bearer token that we're using. I compared this with the Bearer token we obtain on powerbi.com.

 

Both are very similar, however, halfway through they diverge and are different (which could be very well normal depending on how they're generated/reused).

 

We're authenticating where the app owns the data, such as:

 

var credential = new UserPasswordCredential(Username, Password);

// Authenticate using created credentials
var authenticationContext = new AuthenticationContext(AuthorityUrl);
var authenticationResult = await authenticationContext.AcquireTokenAsync(ResourceUrl, ClientId, credential);

If i take the Bearer Token from the above and use it in the request via Postman, I get unauthorized. However, using the same credentials on powerbi.com, I grab the Bearer Token from the network inspector there and there are no issues in Postman with the tokens swapped out (request is OK).

 

I can also confirm the app setup on Azure also has all the permissions available to it.

 

 


@emotix wrote:

Yes, the admin is in the workspace and adding a try/catch just showed the error was Unauthorized.

 

I did some more digging and obtain the Bearer token that we're using. I compared this with the Bearer token we obtain on powerbi.com.

 

Both are very similar, however, halfway through they diverge and are different (which could be very well normal depending on how they're generated/reused).

 

We're authenticating where the app owns the data, such as:

 

var credential = new UserPasswordCredential(Username, Password);

// Authenticate using created credentials
var authenticationContext = new AuthenticationContext(AuthorityUrl);
var authenticationResult = await authenticationContext.AcquireTokenAsync(ResourceUrl, ClientId, credential);

If i take the Bearer Token from the above and use it in the request via Postman, I get unauthorized. However, using the same credentials on powerbi.com, I grab the Bearer Token from the network inspector there and there are no issues in Postman with the tokens swapped out (request is OK).

 

I can also confirm the app setup on Azure also has all the permissions available to it.

 

 


@emotix

Can you double confirm that? Ensure you've checked the same clientid in Azure portal with the clientid in your app owns data sample.

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.