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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Balaji_dev1
Regular Visitor

Access Token expiry quickly or Operation timeout issue.

Hi,

I am able to get access token and able to perform some dataset operations like Add row or get dataset Id.

Problem is that after 2/3 operations/ web calls, it is throwing time operation timeout exception. 

When I check the AuthenticationResult object  - it is showing expiry time well ahead as 1 hrs. But when I run the application it very soon expires say within 2/3 api calls. 

 

Here is my code -

 

private static string GetToken()
{
string clientID = "XXX";

string redirectUri = "https://login.live.com/oauth20_desktop.srf";

string resourceUri = "https://analysis.windows.net/powerbi/api";

string authorityUri = "https://login.windows.net/XXX/oauth2/token";

AuthenticationContext authContext = new AuthenticationContext(authorityUri);
AuthenticationResult authResult = authContext.AcquireToken(resourceUri, clientID, new Uri(redirectUri));
string token = authResult.AccessToken;

Console.WriteLine(token);

return token;
}

 

private static void AddRows(string datasetId, string tableName)
{
string powerBIApiAddRowsUrl = String.Format("https://api.powerbi.com/v1.0/myorg/groups/{0}/datasets/{1}/tables/{2}/rows", group_id, datasetId, tableName);

 

HttpWebRequest request = System.Net.WebRequest.Create(powerBIApiAddRowsUrl) as System.Net.HttpWebRequest;
request.KeepAlive = true;
request.Method = "POST";
request.ContentLength = 0;
request.ContentType = "application/json";

//Add token to the request header
request.Headers.Add("Authorization", String.Format("Bearer {0}", token));

//JSON content for product row
string rowsJson = "{\"rows\":" +
"[{\"ProductID\":10,\"Name\":\"Adjustable Race\",\"Category\":\"Components\",\"IsCompete\":true,\"ManufacturedOn\":\"07/30/2014\"}," +
"{\"ProductID\":10,\"Name\":\"LL Crankarm\",\"Category\":\"Components\",\"IsCompete\":true,\"ManufacturedOn\":\"07/30/2014\"}," +
"{\"ProductID\":10,\"Name\":\"HL Mountain Frame - Silver\",\"Category\":\"Bikes\",\"IsCompete\":true,\"ManufacturedOn\":\"07/30/2014\"}]}";

 

//POST web request
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(rowsJson);
request.ContentLength = byteArray.Length;

//Write JSON byte[] into a Stream

using (Stream writer = request.GetRequestStream())
{
writer.Write(byteArray, 0, byteArray.Length);

try
{
var response = (HttpWebResponse)request.GetResponse();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

Console.WriteLine("Rows Added");}
}

 

 

When I call the AddRow()  in loop say loop of 5, it throws the exception that 'System.Net.WebException: 'The operation has timed out'' for request.GetRequestStream() method after 2/3 call.

What is i am missing. 

Please help.

2 REPLIES 2
Balaji_dev1
Regular Visitor

Actually, it don't have an expiry issue. It is showing expiry datetime 1 hrs.

Anonymous
Not applicable

Hi @Balaji_dev1 

Access Token has expiration time, make sure the size of your data source.

You can try this way to get access token as well.

() =>

let
    body = "client_id=" & #"App ID" 
    & "&scope=https://analysis.windows.net/powerbi/api/.default&client_secret=" & #"App Secret" 
    & "&grant_type=client_credentials",
    Data= Json.Document(Web.Contents("https://login.microsoftonline.com/"& TenantID & "/oauth2/v2.0/token/", 
    [Headers=[#"Content-Type"="application/x-www-form-urlencoded"], Content=Text.ToBinary(body)])),
    access_token = Data[access_token]
in
    access_token

Or you can try this way to set expiration times for your access token.

Configurable token lifetimes in the Microsoft identity platform (preview)

 

Best Regards,

Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. 

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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

Top Solution Authors