-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoauth.js
50 lines (44 loc) · 1.76 KB
/
oauth.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
var SERVICE_NAME = 'strava'
var AUTHORIZATION_BASE_URL = 'https://www.strava.com/oauth/authorize'
var TOKEN_URL = 'https://www.strava.com/oauth/token'
var CALLBACK_FUNCTION_NAME = 'authCallback'
var STRAVA_API_SCOPE = 'activity:read_all'
/** Returns a parameterized OAuth2 Service object. */
function getOAuthService() {
var scriptProps = PropertiesService.getScriptProperties()
var clientId = scriptProps.getProperty('OAUTH_CLIENT_ID')
var clientSecret = scriptProps.getProperty('OAUTH_CLIENT_SECRET')
return OAuth2.createService(SERVICE_NAME)
.setAuthorizationBaseUrl(AUTHORIZATION_BASE_URL)
.setTokenUrl(TOKEN_URL)
.setClientId(clientId)
.setClientSecret(clientSecret)
.setGrantType('offline')
.setPropertyStore(PropertiesService.getUserProperties())
.setScope(STRAVA_API_SCOPE)
.setCallbackFunction(CALLBACK_FUNCTION_NAME)
}
/** The callback that is invoked after an authentication attempt. */
function authCallback(request) {
var authorized = getOAuthService().handleCallback(request)
// close window automatically
var autoclose =
'(Tab should automatically close.) <script>setTimeout(function() { top.window.close() }, 1);</script>'
if (authorized) {
return HtmlService.createHtmlOutput('Success!' + autoclose)
} else {
return HtmlService.createHtmlOutput('Denied.' + autoclose)
}
}
/** Returns {boolean} `true` if successfully authenticated--false otherwise. */
function isAuthValid() {
return getOAuthService().hasAccess()
}
/** Resets the OAuth2 service. */
function resetAuth() {
getOAuthService().reset()
}
/** Returns the 3P authorization urls for the service. */
function get3PAuthorizationUrls() {
return getOAuthService().getAuthorizationUrl()
}