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

Join us at FabCon Vienna from September 15-18, 2025, for the ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM. Get registered

Reply
smpa01
Super User
Super User

Refresh Token Expiry

@peggysue @gbrueckl 

 

I have been runing batch jobs and the token is getting expired ~60/75 min.

 

// header
<CIMultiDictProxy('Content-Length': '104', 'Content-Type': 'application/json; charset=utf-8', 'X-PowerBI-Error-Info': 'TokenExpired', 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains', 'X-Frame-Options': 'deny', 'X-Content-Type-Options': 'nosniff', 'Access-Control-Expose-Headers': 'RequestId,X-PowerBI-Error-Info', 'RequestId': 'f098269c-d1b1-473f-86ae-3da25c0b6555', 'Date': 'Tue, 08 Oct 2024 17:15:34 GMT')>

 

 

I tried this but this is not consistent. I desparately need the bacth job to run without getting terminated in between and without needing me to babysit.

 

 

 

from datetime import datetime, timezone
import pytz
import requests


def current_hour ():
    current_time = datetime.now(pytz.timezone('America/Toronto'))
    hour = current_time.hour
    text_hour = str(hour)
    return text_hour

token = notebookutils.credentials.getToken('pbi')  

service_call = requests.get(uri, headers={"authorization": "Bearer " + token,"VariableHeader": current_hour()}) 

 

 

 

Thank you in advance

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
9 REPLIES 9
Anonymous
Not applicable

HI @smpa01,

Perhaps you can try to modify the session timeout of the notebook, you can click on the left-bottom session icon to open session information panel.

1.png

Regards,

Xiaoxin Sheng

@Anonymous  Thanks for this and I will take this out on a spin and let u know.

 

1 question though - Assuming it works, if I am running batchJob.ipynb with the scheduler at nth hour every day, how can I ensure that it runs with the same custom session timeout (e.g. 90 min) everyday? Asked differently, is the same custom session timeout value applies to the notebook on each run and ca different notebooks have different custom session timeout values ?

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Anonymous
Not applicable

HI @smpa01,

You can open the workspace setting to modify the spark session timeout.

1.png

In addition, you can also consider to use high concurrency mode to running your notebook:
Configure high concurrency mode for notebooks - Microsoft Fabric | Microsoft Learn

Regards,

Xiaoxin Sheng

please give me few days to test it out.

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Anonymous
Not applicable

HI @smpa01,

Sure, you can feel free post here if the above not helps.

Regards,

Xiaoxin Sheng

@Anonymous  if the notebook is run on a schedule, the token expiry still happens. But if I run the notebook is run from while inside it, then the token does not expire.

 

I want to run the notebook on a schedule.

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Anonymous
Not applicable

HI @smpa01,

As you said, it seems like these setting does not enable when you setting schreuder on it. Have you tried to use high concurrency mode to processing your notebook, it should allow to use more resource on it.

Regards,

Xiaoxin Sheng

Unfortunately nothing is working, on scheduled job , the token is getting expired around 63-66 min mark (with 90 min timeout session default). I am also not sure if you can defualt a notebook to run with a HC mode. I can only turn on HC mode when inside it.

 

It is probably because, if I follow this , I get

 

 

{
    "token_type": "Bearer",
    "expires_in": 3599,
    "ext_expires_in": 3599,
    "access_token": "loremIpsum"
}

 

 

How can I refresh this token or get access to a reresh token that can be used to API calls nearing 60 min mark

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Hi @smpa01,

 

When do you create the Bearer token and do you re-use the Bearer token over multiple notebook sessions? If you do, you may retrieve the Bearer token in every session instead, so you are less likely to exceed the 1h limit of the Bearer token.

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 FBC25 Carousel

Fabric Monthly Update - June 2025

Check out the June 2025 Fabric 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.