Greetings,
We're using a rest API to get data from our application into Power BI. Our original query would just retrieve a token and use that token to retrieve a dataset.
let Source = Json.Document(Web.Contents("http://localhost/crm151/bulkapi/Token", [Headers=[username="myusername", password="mypassword", apikey="myapikey"]])), SessionToken = Source[Result][AccessToken], ApiUrl = "http://localhost/crm151/Bulkapi/savedviews/24113", Options = [Headers =[#"sessiontoken"=SessionToken]], Result = Csv.Document(Web.Contents(ApiUrl , Options)) in Result
Next we needed to create a custom data connector so that it is much easier for the end user to use, and give them the option to select their dataset by id.
// This file contains Data Connector logic section DataConnector; [DataSource.Kind="DataConnector", Publish="DataConnector.Publish"] shared DataConnector.Contents = (Url as text, SavedviewId as text) => let Credential = Extension.CurrentCredential(), split = Text.Split(Credential[Username],"#"), Username = List.First(split), Api_key = List.Last(split), Source = Json.Document(Web.Contents(Url&"/bulkapi/token", [Headers=[#"username"=Username, #"password"=Extension.CurrentCredential()[Password], #"apikey"=Api_key]])), SessionToken = Source[Result][AccessToken], ApiUrl = Url&"/Bulkapi/savedviews/"&SavedviewId, Options = [Headers =[#"sessiontoken"=SessionToken]], Result = Csv.Document(Web.Contents(ApiUrl , Options)) in Result; // Data Source Kind description DataConnector = [ Authentication = [ // Key = [], UsernamePassword = [ Label = "Authentication", UsernameLabel = "Username#APIKey (Enter Username and APIKey seperated by '#')" ] // Windows = [], //Implicit = [] ], Label = Extension.LoadString("DataSourceLabel") ]; // Data Source UI publishing description DataConnector.Publish = [ Beta = true, Category = "Other", ButtonText = { Extension.LoadString("ButtonTitle"), Extension.LoadString("ButtonHelp") }, LearnMoreUrl = "https://powerbi.microsoft.com/", SourceImage = DataConnector.Icons, SourceTypeImage = DataConnector.Icons ]; DataConnector.Icons = [ Icon16 = { Extension.Contents("DataConnector16.png"), Extension.Contents("DataConnector20.png"), Extension.Contents("DataConnector24.png"), Extension.Contents("DataConnector32.png") }, Icon32 = { Extension.Contents("DataConnector32.png"), Extension.Contents("DataConnector40.png"), Extension.Contents("DataConnector48.png"), Extension.Contents("DataConnector64.png") } ];
At first this works perfectly fine. However, after session tokens start expiring we run into issues. When I look at Fiddler, I noticed that it's using an old session token and not retrieving a fresh session token. Just to make sure it's not an issue on my API's end, I ran the original query script and it worked fine. When I clear Power BI's cache, the data connector works again, however, once the session tokens start to expire I run into the same issues.
Is there a way to make sure the data connector is retrieving a fresh session token each time and not refer to cached information? Any suggestions would be greatly appreciated.