The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
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.
Actually, it don't have an expiry issue. It is showing expiry datetime 1 hrs.
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.