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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
remeez
Frequent Visitor

Operation returned an invalid status code 'Forbidden' GenerateTokenInGroupAsync

Hi All,

 

I am getting an error while executing the GenerateTokenInGroupAsync().  The error "Operation returned an invalid status code 'Forbidden'".  

 

I had users using the system all day, and the late afternoon this just stopped working.  I am using the following method to get the access token.

 

 

public static async Task<AccessToken> SetAccessToken()
        {
            List<KeyValuePair<string, string>> vals = new List<KeyValuePair<string, string>>
            {
                new KeyValuePair<string, string>("grant_type", "password"),
                new KeyValuePair<string, string>("scope", "openid"),
                new KeyValuePair<string, string>("resource", "https://analysis.windows.net/powerbi/api"),
                new KeyValuePair<string, string>("client_id", "XXXXX"),
                new KeyValuePair<string, string>("client_secret", "XXXXX"),
                new KeyValuePair<string, string>("username", "XXX"),
                new KeyValuePair<string, string>("password", "XXX")
            };
            string TenantId = "XXX";
            string url = string.Format("https://login.windows.net/{0}/oauth2/token", TenantId);
            HttpClient hc = new HttpClient();
            HttpContent content = new FormUrlEncodedContent(vals);
            HttpResponseMessage hrm = hc.PostAsync(url, content).Result;
            string responseData = "";
            if (hrm.IsSuccessStatusCode)
            {
                Stream data = await hrm.Content.ReadAsStreamAsync();
                using (StreamReader reader = new StreamReader(data, Encoding.UTF8))
                {
                    responseData = reader.ReadToEnd();
                }
            }
            AccessToken token = JsonConvert.DeserializeObject<AccessToken>(responseData);

            return token;
        }

I can retrieve the report details using client.Reports.GetReportInGroupAsync().

 

 

 

var report = await client.Reports.GetReportInGroupAsync(groupId, reportId, CancellationToken.None);
                    
var generateTokenRequestParamaters = new GenerateTokenRequest(TokenAccessLevel.View, datasetId, allowSaveAs: null);
var tokenResponse = await client.Reports.GenerateTokenInGroupAsync(groupId, report.Id, generateTokenRequestParamaters, CancellationToken.None);

The error occurs when executing the last line of code in the code above.

 

Regards

Remeez

 

1 ACCEPTED SOLUTION

Hi @v-micsh-msft,

 

Sorry for the late response, the reason I was getting the error was because my account ran out of free token requests.  I had to switch on dedicated capacity in order to continue.

 

Regards

Remeez

View solution in original post

5 REPLIES 5
v-micsh-msft
Microsoft Employee
Microsoft Employee

Hi @remeez,

 

Please first follow the steps mentioned by Eric_Zhang in the following thread:

https://community.powerbi.com/t5/Developer/Generate-PowerBI-Embed-Token-gives-me-Forbidden-500-Inter...

 

Regards,

Michael

Hi @v-micsh-msft,

 

Sorry for the late response, the reason I was getting the error was because my account ran out of free token requests.  I had to switch on dedicated capacity in order to continue.

 

Regards

Remeez

Anonymous
Not applicable

Hi. I'm having the same problem and I can't figure it out. I'm wondering if it's for the same reason as you. How would I check how many free token requests I have left?

Hi, I might have the same problem. Did you find a way to identify it? I mean, Any way to check the amount of token requests left?

I am also looking to find out the number of token requests left. Were you able to find it?

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.