1+ var CLIENT_ID = '...' ;
2+ var CLIENT_SECRET = '...' ;
3+
4+ /**
5+ * Authorizes and makes a request to the Google+ API.
6+ */
7+ function run ( ) {
8+ var service = getService ( ) ;
9+ if ( service . hasAccess ( ) ) {
10+ var url = 'https://www.googleapis.com/plus/v1/people/me' ;
11+ var response = UrlFetchApp . fetch ( url , {
12+ headers : {
13+ Authorization : 'Bearer ' + service . getAccessToken ( )
14+ }
15+ } ) ;
16+ var result = JSON . parse ( response . getContentText ( ) ) ;
17+ Logger . log ( JSON . stringify ( result , null , 2 ) ) ;
18+ } else {
19+ var authorizationUrl = service . getAuthorizationUrl ( ) ;
20+ Logger . log ( 'Open the following URL and re-run the script: %s' ,
21+ authorizationUrl ) ;
22+ }
23+ }
24+
25+ /**
26+ * Reset the authorization state, so that it can be re-tested.
27+ */
28+ function reset ( ) {
29+ var service = getService ( ) ;
30+ service . reset ( ) ;
31+ }
32+
33+ /**
34+ * Configures the service.
35+ */
36+ function getService ( ) {
37+ return OAuth2 . createService ( 'GooglePlus' )
38+ // Set the endpoint URLs.
39+ . setAuthorizationBaseUrl ( 'https://accounts.google.com/o/oauth2/auth' )
40+ . setTokenUrl ( 'https://accounts.google.com/o/oauth2/token' )
41+
42+ // Set the client ID and secret.
43+ . setClientId ( CLIENT_ID )
44+ . setClientSecret ( CLIENT_SECRET )
45+
46+ // Set the name of the callback function that should be invoked to complete
47+ // the OAuth flow.
48+ . setCallbackFunction ( 'authCallback' )
49+
50+ // Set the property store where authorized tokens should be persisted.
51+ . setPropertyStore ( PropertiesService . getUserProperties ( ) )
52+
53+ // Set the scope and additional Google-specific parameters.
54+ . setScope ( 'profile' )
55+ . setParam ( 'access_type' , 'offline' )
56+ . setParam ( 'approval_prompt' , 'force' )
57+ . setParam ( 'login_hint' , Session . getActiveUser ( ) . getEmail ( ) ) ;
58+ }
59+
60+ /**
61+ * Handles the OAuth callback.
62+ */
63+ function authCallback ( request ) {
64+ var service = getService ( ) ;
65+ var authorized = service . handleCallback ( request ) ;
66+ if ( authorized ) {
67+ return HtmlService . createHtmlOutput ( 'Success!' ) ;
68+ } else {
69+ return HtmlService . createHtmlOutput ( 'Denied' ) ;
70+ }
71+ }
0 commit comments