-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
245 lines (211 loc) · 10.8 KB
/
Copy pathapp.js
File metadata and controls
245 lines (211 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
* See LICENSE in the project root for license information.
*/
'use strict';
(function() {
// create
angular
.module('outlook-2-sp', [])
.controller('HomeController', [HomeController])
.config(['$logProvider', function($logProvider) {
// set debug logging to on
if ($logProvider.debugEnabled) {
$logProvider.debugEnabled(true);
}
}]);
/**
* Home Controller
*/
function HomeController() {
var _this = this;
var timesGetOneDriveFilesHasRun = 0;
var triedWithoutForceConsent = false;
_this.title = 'Home';
_this.baseRestHost = Office.context.mailbox.restUrl;
// Displays the data, assumed to be an array.
function showResult(data) {
_this.data = data;
}
function logError(error) {
console.log(error);
}
function handleClientSideErrors(result) {
switch (result.error.code) {
// Handle the case where user is not logged in, or the user cancelled, without responding, a
// prompt to provide a 2nd authentication factor.
case 13001:
getDataWithToken({ forceAddAccount: true });
break;
// Handle the case where the user's sign-in or consent was aborted.
case 13002:
if (timesGetOneDriveFilesHasRun < 2) {
showResult(['Your sign-in or consent was aborted before completion. Please try that operation again.']);
} else {
logError(result);
}
break;
// Handle the case where the user is logged in with an account that is neither work or school,
// nor Micrososoft Account.
case 13003:
showResult(['Please sign out of Office and sign in again with a work or school account, or Microsoft Account. Other kinds of accounts, like corporate domain accounts do not work.']);
break;
case 13005:
getDataWithToken({ forceConsent: true });
break;
// Handle an unspecified error from the Office host.
case 13006:
showResult(['Please save your work, sign out of Office, close all Office applications, and restart this Office application.']);
break;
// Handle the case where the Office host cannot get an access token to the add-ins
// web service/application.
case 13007:
showResult(['That operation cannot be done at this time. Please try again later.']);
break;
// Handle the case where the user tiggered an operation that calls `getAccessTokenAsync`
// before a previous call of it completed.
case 13008:
showResult(['Please try that operation again after the current operation has finished.']);
break;
// Handle the case where the add-in does not support forcing consent.
case 13009:
if (triedWithoutForceConsent) {
showResult(['Please sign out of Office and sign in again with a work or school account, or Microsoft Account.']);
} else {
getDataWithToken({ forceConsent: false });
}
break;
// Log all other client errors.
default:
logError(result);
break;
}
}
// function handleServerSideErrors(result) {
// // TODO10: Handle the case where AAD asks for an additional form of authentication.
// if (result.responseJSON.error.innerError &&
// result.responseJSON.error.innerError.error_codes &&
// result.responseJSON.error.innerError.error_codes[0] === 50076) {
// getDataWithToken({ authChallenge: result.responseJSON.error.innerError.claims });
// }
// // TODO11: Handle the case where consent has not been granted, or has been revoked.
// else if (result.responseJSON.error.innerError &&
// result.responseJSON.error.innerError.error_codes &&
// result.responseJSON.error.innerError.error_codes[0] === 65001) {
// getDataWithToken({ forceConsent: true });
// }
// // TODO12: Handle the case where an invalid scope (permission) was used in the on-behalf-of flow
// else if (result.responseJSON.error.innerError &&
// result.responseJSON.error.innerError.error_codes &&
// result.responseJSON.error.innerError.error_codes[0] === 70011) {
// showResult(['The add-in is asking for a type of permission that is not recognized.']);
// }
// // TODO13: Handle the case where the token that the add-in's client-side sends to it's
// // server-side is not valid because it is missing `access_as_user` scope (permission).
// else if (result.responseJSON.error.name &&
// result.responseJSON.error.name.indexOf('expected access_as_user') !== -1) {
// showResult(['Microsoft Office does not have permission to get Microsoft Graph data on behalf of the current user.']);
// }
// // TODO14: Handle the case where the token sent to Microsoft Graph in the request for
// // data is expired or invalid.
// else if (result.responseJSON.error.name &&
// result.responseJSON.error.name.indexOf('Microsoft Graph error') !== -1) {
// if (!timesMSGraphErrorReceived) {
// timesMSGraphErrorReceived = true;
// timesGetOneDriveFilesHasRun = 0;
// triedWithoutForceConsent = false;
// getOneDriveFiles();
// } else {
// logError(result);
// }
// }
// // TODO15: Log all other server errors.
// else {
// logError(result);
// }
// }
function getDataWithToken(options) {
Office.context.auth.getAccessTokenAsync(options,
function(result) {
if (result.status === "succeeded") {
console.log('accesstokenasync', result.value);
var getTokenUrl = 'https://login.microsoftonline.com/common/oauth2/v2.0/token?grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&client_id=68ed1183-98b6-4eff-a9da-3ced59f61d23&client_secret=uaxITU03?{dzpxQRJF979:!&assertion=' + result.value + '&scope=user.read sites.read.all profile&requested_token_use=on_behalf_of'
$.ajax({
url: getTokenUrl,
method: 'POST'
}).done(function(item) {
console.log(item);
}).fail(function(error) {
console.log(error)
});
} else {
handleClientSideErrors(result);
}
});
}
triedWithoutForceConsent = true;
getDataWithToken({ forceConsent: false });
Office.context.mailbox.getCallbackTokenAsync({ isRest: true }, function(result) {
if (result.status === "succeeded") {
_this.accessToken = result.value;
// Use the access token
console.log(_this.accessToken);
} else {
// Handle the error
}
});
_this.run = function() {
function getItemRestId() {
if (Office.context.mailbox.diagnostics.hostName === 'OutlookIOS') {
// itemId is already REST-formatted
return Office.context.mailbox.item.itemId;
} else {
// Convert to an item ID for API v2.0
return Office.context.mailbox.convertToRestId(
Office.context.mailbox.item.itemId,
Office.MailboxEnums.RestVersion.v2_0
);
}
}
function getCurrentThread(accessToken, threadId) {
// Construct the REST URL to the current item
// Details for formatting the URL can be found at
// https://msdn.microsoft.com/office/office365/APi/mail-rest-operations#get-a-message-rest
var getMessageUrl = _this.baseRestHost + '/v2.0/me/messages/?$filter=conversationId eq \'' + threadId + '\'&$top=20';
$.ajax({
url: getMessageUrl,
dataType: 'json',
headers: { 'Authorization': 'Bearer ' + accessToken }
}).done(function(item) {
console.log(item);
triedWithoutForceConsent = true;
getDataWithToken({ forceConsent: false });
}).fail(function(error) {
// Handle error
});
}
function getCurrentItem(accessToken) {
// Get the item's REST ID
var itemId = getItemRestId();
// Construct the REST URL to the current item
// Details for formatting the URL can be found at
// https://msdn.microsoft.com/office/office365/APi/mail-rest-operations#get-a-message-rest
var getMessageUrl = _this.baseRestHost + '/v2.0/me/messages/' + itemId;
$.ajax({
url: getMessageUrl,
dataType: 'json',
headers: { 'Authorization': 'Bearer ' + accessToken }
}).done(function(item) {
getCurrentThread(accessToken, item.ConversationId);
}).fail(function(error) {
// Handle error
});
}
getCurrentItem(_this.accessToken);
}
}
// when Office has initalized, manually bootstrap the app
Office.initialize = function() {
angular.bootstrap(document.body, ['outlook-2-sp']);
};
})();