forked from OfficeDev/Microsoft-Teams-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleGraphClient.js
92 lines (78 loc) · 3.3 KB
/
simpleGraphClient.js
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const { Client } = require('@microsoft/microsoft-graph-client');
/**
* This class is a wrapper for the Microsoft Graph API.
* See: https://developer.microsoft.com/en-us/graph for more information.
*/
class SimpleGraphClient {
constructor(token) {
if (!token || !token.trim()) {
throw new Error('SimpleGraphClient: Invalid token received.');
}
this._token = token;
// Get an Authenticated Microsoft Graph client using the token issued to the user.
this.graphClient = Client.init({
authProvider: (done) => {
done(null, this._token); // First parameter takes an error if you can't get an access token.
},
});
}
/**
*
* @param {string} searchQuery text to search the inbox for.
*/
async searchMailInbox(searchQuery) {
// Searches the user's mail Inbox using the Microsoft Graph API
return await this.graphClient
.api(`me/messages?$search=\"${searchQuery}\"`)
.get();
}
async GetMyProfile() {
return await this.graphClient.api('/me').get();
}
// // Gets the user's photo
// async GetPhotoAsync() {
// // let graphRequestParams = {
// // method: 'GET',
// // headers: {
// // 'Content-Type': 'image/png',
// // "authorization": "bearer " + token
// // }
// // }
// const header = {
// 'Content-Type': 'image/png'
// }
// let result = await this.graphClient.api('/me/photos/240x240/$value').headers(header).get();
// //let graphPhotoEndpoint = 'https://graph.microsoft.com/v1.0/me/photos/240x240/$value';
// //let response = await fetch(graphPhotoEndpoint, graphRequestParams).catch(this.unhandledFetchError);
// if (!result) {
// console.error("ERROR getting user photo!");
// }
// //let imageBuffer = await result.arrayBuffer().catch(this.unhandledFetchError); //Get image data as raw binary data
// //const imageBuffer = result.
// //Convert binary data to an image URL and set the url in state
// const imageUri = 'data:image/png;base64,' + Buffer.from(result).toString('base64');
// return imageUri;
// }
// Gets the user's photo
async GetPhotoAsync(token) {
let graphPhotoEndpoint = 'https://graph.microsoft.com/v1.0/me/photos/240x240/$value';
let graphRequestParams = {
method: 'GET',
headers: {
'Content-Type': 'image/png',
"authorization": "bearer " + token
}
}
let response = await fetch(graphPhotoEndpoint, graphRequestParams).catch(this.unhandledFetchError);
if (!response.ok) {
console.error("ERROR: ", response);
}
let imageBuffer = await response.arrayBuffer().catch(this.unhandledFetchError); //Get image data as raw binary data
//Convert binary data to an image URL and set the url in state
const imageUri = 'data:image/png;base64,' + Buffer.from(imageBuffer).toString('base64');
return imageUri;
}
}
exports.SimpleGraphClient = SimpleGraphClient;