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
{{ message }}
This repository was archived by the owner on Mar 13, 2020. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+124-8Lines changed: 124 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -168,14 +168,14 @@ This doesn't do anything but display the authorization code returned by Azure, b
168
168
169
169
Now let's add a new function to the `oAuthService` class to retrieve a token. Add the following function to the class in the `./oauth.php` file.
170
170
171
-
#### New `getTokenFromAuthCode` function in `./oauth.php` ####
171
+
#### New `getToken` function in `./oauth.php` ####
172
172
173
173
```PHP
174
-
public static function getTokenFromAuthCode($authCode, $redirectUri) {
174
+
public static function getToken($grantType, $code, $redirectUri) {
175
175
// Build the form data to post to the OAuth2 token endpoint
176
176
$token_request_data = array(
177
-
"grant_type" => "authorization_code",
178
-
"code" => $authCode,
177
+
"grant_type" => $grantType,
178
+
"code" => $code,
179
179
"redirect_uri" => $redirectUri,
180
180
"scope" => implode(" ", self::$scopes),
181
181
"client_id" => self::$clientId,
@@ -228,7 +228,17 @@ public static function getTokenFromAuthCode($authCode, $redirectUri) {
228
228
229
229
This function uses cURL to issue the access token request to login.microsoftonline.com. The first part of this function is building the payload of the request, then the rest is using cURL to issue a POST request to the OAuth2 token endpoint. Finally, the results are parsed into an array of values using `json_decode`.
230
230
231
-
Let's see if this works. Replace the contents of the `./authorize.php` file with the following.
231
+
Now add a wrapper function to pass the correct grant type in the `$grantType` parameter for exchanging an authorization code for a token.
232
+
233
+
#### New `getTokenFromAuthCode` function in `./oauth.php` ####
234
+
235
+
```PHP
236
+
public static function getTokenFromAuthCode($authCode, $redirectUri) {
Structuring the code this way will allow us to reuse the `getToken` function later. Let's see if this works. Replace the contents of the `./authorize.php` file with the following.
232
242
233
243
#### Updated contents of `./authorize.php` ####
234
244
@@ -441,7 +451,113 @@ Finally, let's update the `./home.php` file to check for the presence of the acc
441
451
</html>
442
452
```
443
453
444
-
Now that we have an access token, we're ready to use the Mail API.
454
+
### Refreshing the access token
455
+
456
+
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.
457
+
458
+
In order to do that, the app must request the `offline_access` scope. Add this scope to the `$scopes` array in `oauth.php`:
459
+
460
+
```PHP
461
+
// The scopes the app requires
462
+
private static $scopes = array("openid",
463
+
"offline_access",
464
+
"https://outlook.office.com/mail.read");
465
+
```
466
+
467
+
This will cause the token response from Azure to include a refresh token. Let's update `authorize.php` to save the refresh token and the expiration time in a session cookie.
This method checks the expiration time. If the current time is greater than the expiration, it calls our `getTokenFromRefreshToken` function to refresh. Otherwise, it just returns the cached token.
559
+
560
+
Now that we have an access token and we can refresh if needed, we're ready to use the Mail API.
445
561
446
562
## Using the Mail API ##
447
563
@@ -501,7 +617,7 @@ Update `./home.php` to call the `getMessages` function and display the results.
0 commit comments