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.
// The app only needs openid (for user's ID info), and Mail.Read
88
+
private static $scopes = array("openid",
89
+
"https://outlook.office.com/mail.read");
89
90
90
91
public static function getLoginUrl($redirectUri, $scopes) {
91
92
// Build scope string. Multiple scopes are separated
@@ -138,7 +139,7 @@ The class exposes one function for now, `getLoginUrl`. This function will genera
138
139
139
140
Save your work and copy the files to your web server. If you browse to `http://localhost/php-tutorial/home.php` and hover over the sign in link, it should look like this:
Clicking on the link will allow you to sign in and grant access to the app, but will then result in an error. Notice that your browser gets redirected to `http://localhost/php-tutorial/authorize.php`. That file doesn't exist yet. Don't worry, that's our next task.
144
145
@@ -217,6 +218,33 @@ Now let's add a new function to the `oAuthService` class to retrieve a token. Ad
217
218
218
219
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`.
219
220
221
+
### Getting the user's email address ###
222
+
223
+
The array of values returned doesn't only include the access token. Since we included the `openid` scope in our request, it also contains in ID token. We can use this token to find out a few pieces of information about the logged on user. In this case, we want to get the user's email address. You'll see why we want this soon.
224
+
225
+
Add a new function to the `oAuthService` class called `getUserEmailFromIdToken`.
226
+
227
+
#### New `getUserEmailFromIdToken` function in `./oauth.php` ####
228
+
229
+
public static function getUserEmailFromIdToken($idToken) {
230
+
error_log("ID TOKEN: ".$idToken);
231
+
232
+
// JWT is made of three parts, separated by a '.'
233
+
// First part is the header
234
+
// Second part is the token
235
+
// Third part is the signature
236
+
$token_parts = explode(".", $idToken);
237
+
238
+
// We care about the token
239
+
// URL decode first
240
+
$token = strtr($token_parts[1], "-_", "+/");
241
+
// Then base64 decode
242
+
$jwt = base64_decode($token);
243
+
// Finally parse it as JSON
244
+
$json_token = json_decode($jwt, true);
245
+
return $json_token['preferred_username'];
246
+
}
247
+
220
248
Now replace the contents of the `./authorize.php` file with the following.
221
249
222
250
#### Updated contents of `./authorize.php` ####
@@ -232,7 +260,7 @@ Now replace the contents of the `./authorize.php` file with the following.
Save your changes and restart the app. This time, after you sign in, you should see an access token displayed. Now let's update `./authorize.php` one more time to save the access token into a session variable and redirect back to the home page.
263
+
Save your changes and restart the app. This time, after you sign in, you should see an access token displayed. Now let's update `./authorize.php` one more time to get the user's email address, save the access token and email address into session variables, and redirect back to the home page.
236
264
237
265
#### Updated contents of `./authorize.php` ####
238
266
@@ -246,6 +274,10 @@ Save your changes and restart the app. This time, after you sign in, you should
"client-request-id: ".self::makeGuid(), // Stamp each new request with a new GUID.
310
-
"return-client-request-id: true" // Tell the server to include our request-id GUID in the response.
342
+
"return-client-request-id: true", // Tell the server to include our request-id GUID in the response.
343
+
"X-AnchorMailbox: ".$user_email // Provider user's email to optimize routing of API call
311
344
);
312
345
313
346
$curl = curl_init($url);
@@ -393,15 +426,17 @@ Let's start by adding a new file to contain all of our Mail API functions called
393
426
}
394
427
?>
395
428
396
-
This function uses cURL to send the appropriate request to the specified endpoint, using the access token for authentication. We can use this function to call any of Outlook REST APIs. Let's add a new function to the `OutlookService` class to get the user's 10 most recent messages from the inbox.
429
+
This function uses cURL to send the appropriate request to the specified endpoint, using the access token for authentication. It also uses the user's email address for an important optimization. By setting the `X-AnchorMailbox` header to the user's email address, the API endpoint can route API calls to the correct backend mailbox servers more efficiently.
430
+
431
+
We can use this function to call any of Outlook REST APIs. Let's add a new function to the `OutlookService` class to get the user's 10 most recent messages from the inbox.
397
432
398
-
In order to call our new `makeApiCall` function, we need an access token, a method, a URL, and an optional payload. We already have the access token, and from the [Mail API Reference](https://msdn.microsoft.com/office/office365/APi/mail-rest-operations#GetMessages), we know that the method to get messages is `GET` and that the URL to get messages is `https://outlook.office.com/api/v1.0/me/messages`. Using that information, add a `getMessages` function in `outlook.php`.
433
+
In order to call our new `makeApiCall` function, we need an access token, the user's email address, a method, a URL, and an optional payload. We already have the access token, and from the [Mail API Reference](https://msdn.microsoft.com/office/office365/APi/mail-rest-operations#GetMessages), we know that the method to get messages is `GET` and that the URL to get messages is `https://outlook.office.com/api/v1.0/me/messages`. Using that information, add a `getMessages` function in `outlook.php`.
399
434
400
435
#### New `getMessages` function in `./outlook.php` ####
0 commit comments