Skip to content
This repository was archived by the owner on Mar 13, 2020. It is now read-only.

Commit bfdd899

Browse files
committed
Updated sample for latest simple-oauth2
Latest simple-oauth2 introduced a breaking change, so updated to reflect the latest.
1 parent a0ad642 commit bfdd899

4 files changed

Lines changed: 29 additions & 21 deletions

File tree

‎README.md‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,16 @@ Now the library is installed and ready to use. Create a new file called `authHel
136136

137137
```js
138138
var credentials = {
139-
clientID: 'YOUR APP ID HERE',
140-
clientSecret: 'YOUR APP PASSWORD HERE',
141-
site: 'https://login.microsoftonline.com/common',
142-
authorizationPath: '/oauth2/v2.0/authorize',
143-
tokenPath: '/oauth2/v2.0/token'
144-
}
139+
client: {
140+
id: 'YOUR APP ID HERE',
141+
secret: 'YOUR APP PASSWORD HERE',
142+
},
143+
auth: {
144+
tokenHost: 'https://login.microsoftonline.com',
145+
authorizePath: 'common/oauth2/v2.0/authorize',
146+
tokenPath: 'common/oauth2/v2.0/token'
147+
}
148+
};
145149
var oauth2 = require('simple-oauth2')(credentials);
146150

147151
var redirectUri = 'http://localhost:8000/authorize';
@@ -151,7 +155,7 @@ var scopes = [ 'openid',
151155
'https://outlook.office.com/mail.read' ];
152156

153157
function getAuthUrl() {
154-
var returnVal = oauth2.authCode.authorizeURL({
158+
var returnVal = oauth2.authorizationCode.authorizeURL({
155159
redirect_uri: redirectUri,
156160
scope: scopes.join(' ')
157161
});
@@ -162,7 +166,7 @@ function getAuthUrl() {
162166
exports.getAuthUrl = getAuthUrl;
163167
```
164168

165-
The first thing we do here is define our client ID and secret. We also define a redirect URI and an array of scopes. The scope array includes the `openid` and `Mail.Read` scopes, since we will only read the user's mail. The values of `clientId` and `clientSecret` are just placeholders, so we need to generate valid values.
169+
The first thing we do here is define our client ID and secret. We also define a redirect URI and an array of scopes. The scope array includes the `openid` and `Mail.Read` scopes, since we will only read the user's mail. The values of `id` and `secret` are just placeholders, so we need to generate valid values.
166170

167171
### Generate a client ID and secret ###
168172

@@ -260,7 +264,7 @@ Let's add another helper function to `authHelper.js` called `getTokenFromCode`.
260264
```js
261265
function getTokenFromCode(auth_code, callback, response) {
262266
var token;
263-
oauth2.authCode.getToken({
267+
oauth2.authorizationCode.getToken({
264268
code: auth_code,
265269
redirect_uri: redirectUri,
266270
scope: scopes.join(' ')
@@ -459,7 +463,7 @@ Now let's add a helper function in `index.js` to retrieve the cached token, chec
459463
function getAccessToken(request, response, callback) {
460464
var expiration = new Date(parseFloat(getValueFromCookie('node-tutorial-token-expires', request.headers.cookie)));
461465

462-
if (Date.compare(expiration, new Date()) === -1) {
466+
if (expiration <= new Date()) {
463467
// refresh token
464468
console.log('TOKEN EXPIRED, REFRESHING');
465469
var refresh_token = getValueFromCookie('node-tutorial-refresh-token', request.headers.cookie);

‎authHelper.js‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
// Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license. See full license at the bottom of this file.
22
var credentials = {
3-
clientID: 'YOUR APP ID HERE',
4-
clientSecret: 'YOUR APP PASSWORD HERE',
5-
site: 'https://login.microsoftonline.com/common',
6-
authorizationPath: '/oauth2/v2.0/authorize',
7-
tokenPath: '/oauth2/v2.0/token'
8-
}
9-
var oauth2 = require('simple-oauth2')(credentials)
3+
client: {
4+
id: 'YOUR APP ID HERE',
5+
secret: 'YOUR APP PASSWORD HERE',
6+
},
7+
auth: {
8+
tokenHost: 'https://login.microsoftonline.com',
9+
authorizePath: 'common/oauth2/v2.0/authorize',
10+
tokenPath: 'common/oauth2/v2.0/token'
11+
}
12+
};
13+
var oauth2 = require('simple-oauth2').create(credentials);
1014

1115
var redirectUri = 'http://localhost:8000/authorize';
1216

@@ -18,7 +22,7 @@ var scopes = [ 'openid',
1822
'https://outlook.office.com/contacts.read' ];
1923

2024
function getAuthUrl() {
21-
var returnVal = oauth2.authCode.authorizeURL({
25+
var returnVal = oauth2.authorizationCode.authorizeURL({
2226
redirect_uri: redirectUri,
2327
scope: scopes.join(' ')
2428
});
@@ -28,7 +32,7 @@ function getAuthUrl() {
2832

2933
function getTokenFromCode(auth_code, callback, response) {
3034
var token;
31-
oauth2.authCode.getToken({
35+
oauth2.authorizationCode.getToken({
3236
code: auth_code,
3337
redirect_uri: redirectUri,
3438
scope: scopes.join(' ')

‎index.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function getValueFromCookie(valueName, cookie) {
8686
function getAccessToken(request, response, callback) {
8787
var expiration = new Date(parseFloat(getValueFromCookie('node-tutorial-token-expires', request.headers.cookie)));
8888

89-
if (Date.compare(expiration, new Date()) === -1) {
89+
if (expiration <= new Date()) {
9090
// refresh token
9191
console.log('TOKEN EXPIRED, REFRESHING');
9292
var refresh_token = getValueFromCookie('node-tutorial-refresh-token', request.headers.cookie);

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
],
1313
"dependencies": {
1414
"node-outlook": "^1.1.6",
15-
"simple-oauth2": "^0.7.0"
15+
"simple-oauth2": "^1.0.1"
1616
}
1717
}

0 commit comments

Comments
 (0)