You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+110-4Lines changed: 110 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -138,6 +138,9 @@ Now the library is installed and ready to use. Create a new file in the `tutoria
138
138
139
139
```python
140
140
from urllib.parse import quote, urlencode
141
+
import base64
142
+
import json
143
+
import time
141
144
142
145
# Client ID and secret
143
146
client_id ='YOUR APP ID HERE'
@@ -375,7 +378,110 @@ def gettoken(request):
375
378
376
379
If you save your changes, restart the server, and go through the sign-in process again, you should now see a long string of seemingly nonsensical characters. If everything's gone according to plan, that should be an access token.
377
380
378
-
Now we're ready to call the Mail API.
381
+
### Refreshing the access token
382
+
383
+
Access tokens returned from Azure are valid for an hour. If you use the token after it has expired, the API calls will return 401 errors. You could ask the user to sign in again, but the better option is to refresh the token silently.
384
+
385
+
In order to do that, the app must request the `offline_access` scope. Add this scope to the `scopes` array in `authhelper.py`:
386
+
387
+
```python
388
+
# The scopes required by the app
389
+
scopes = [ 'openid',
390
+
'offline_access',
391
+
'https://outlook.office.com/mail.read' ]
392
+
```
393
+
394
+
This will cause the token response from Azure to include a refresh token. Let's update `gettoken` in `views.py` to save the refresh token and the expiration time in the session.
395
+
396
+
#### Updated `gettoken` function in `.\tutorial\views.py` ####
Finally let's create a helper function to retrieve the access token. The function will check the expiration time, and if the token is expired, will refresh it. Otherwise it will just return the access token from the session. Add the following function to `authhelper.py`.
449
+
450
+
#### The `get_access_token` function in `./tutorial/authhelper.py` ####
0 commit comments