forked from benlcollins/nestThermostatLogger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Oauth2.js
92 lines (68 loc) · 2.8 KB
/
Oauth2.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
/**
* Create the OAuth 2 service
*/
function getSmartService() {
// Create a new service with the given name. The name will be used when
// persisting the authorized token, so ensure it is unique within the
// scope of the property store.
return OAuth2.createService('smd')
// Set the endpoint URLs, which are the same for all Google services.
.setAuthorizationBaseUrl(`https://nestservices.google.com/partnerconnections/${PROJECT_ID}/auth`)
.setTokenUrl('https://www.googleapis.com/oauth2/v4/token')
// Set the client ID and secret, from the Google Developers Console.
.setClientId(OAUTH_CLIENT_ID)
.setClientSecret(OAUTH_CLIENT_SECRET)
// Set the name of the callback function in the script referenced
// above that should be invoked to complete the OAuth flow.
.setCallbackFunction('authCallback')
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getUserProperties())
// Set the scopes to request (space-separated for Google services).
.setScope('https://www.googleapis.com/auth/sdm.service')
// Below are Google-specific OAuth2 parameters.
// Sets the login hint, which will prevent the account chooser screen
// from being shown to users logged in with multiple accounts.
.setParam('login_hint', Session.getEffectiveUser().getEmail())
// Requests offline access.
.setParam('access_type', 'offline')
// Consent prompt is required to ensure a refresh token is always
// returned when requesting offline access.
.setParam('prompt', 'consent');
}
/**
* Direct the user to the authorization URL
*/
function showAuthorizationSidebar() {
const smartService = getSmartService();
if (!smartService.hasAccess()) {
// App does not have access yet
const authorizationUrl = smartService.getAuthorizationUrl();
const template = HtmlService.createTemplate(
'<a href="<?= authorizationUrl ?>" target="_blank">Authorize</a>. ' +
'Reopen the sidebar when the authorization is complete.');
template.authorizationUrl = authorizationUrl;
const page = template.evaluate();
SpreadsheetApp.getUi().showSidebar(page);
} else {
// App has access
console.log('App has access');
// make the API request
getDevices();
}
}
/**
* Handle the callback
*/
function authCallback(request) {
const smartService = getSmartService();
const isAuthorized = smartService.handleCallback(request);
if (isAuthorized) {
return HtmlService.createHtmlOutput('Success! You can close this tab.');
} else {
return HtmlService.createHtmlOutput('Denied. You can close this tab');
}
}
function refreshToken() {
const smartService = getSmartService();
smartService.refresh();
}