44 * https://developers.facebook.com/apps/
55 */
66
7+ var CLIENT_ID = '...' ;
8+ var CLIENT_SECRET = '...' ;
9+
710/*
811 * Authorizes and makes a request to the Facebook API.
912 */
1013
1114function run ( e ) {
12- var service = getService ( ) ;
13- var html = '' ;
14- if ( service . hasAccess ( ) ) {
15- // Takes info about the id 100002297950397
16- var url = 'https://graph.facebook.com/v2.6/100002297950397' ;
17- var response = UrlFetchApp . fetch ( url , {
18- headers : {
19- 'Authorization' : 'Bearer ' + service . getAccessToken ( )
20- }
21- } ) ;
22- var contentText = JSON . stringify ( JSON . parse ( response . getContentText ( ) ) , null , ' ' ) ;
23- html = Utilities . formatString ( '<pre>%s</pre>' , contentText ) ;
24- } else {
25- var authorizationUrl = service . getAuthorizationUrl ( ) ;
26- html = Utilities . formatString ( 'Open the following URL and re-run the script: <a href="%s" target="_blank">[+]</a>' , authorizationUrl ) ;
27- }
28- return HtmlService . createHtmlOutput ( html ) ;
15+ var service = getService ( ) ;
16+ var html = '' ;
17+ if ( service . hasAccess ( ) ) {
18+ // Takes info about the id 100002297950397
19+ var url = 'https://graph.facebook.com/v2.6/100002297950397' ;
20+ var response = UrlFetchApp . fetch ( url , {
21+ headers : {
22+ 'Authorization' : 'Bearer ' + service . getAccessToken ( )
23+ }
24+ } ) ;
25+ var result = JSON . parse ( response . getContentText ( ) ) ;
26+ Logger . log ( JSON . stringify ( result , null , 2 ) ) ;
27+ } else {
28+ var authorizationUrl = service . getAuthorizationUrl ( ) ;
29+ Logger . log ( 'Open the following URL and re-run the script: %s' ,
30+ authorizationUrl ) ;
31+ }
2932}
3033
3134/**
3235 * Reset the authorization state, so that it can be re-tested.
3336 */
3437function reset ( ) {
35- var service = getService ( ) ;
36- service . reset ( ) ;
38+ var service = getService ( ) ;
39+ service . reset ( ) ;
3740}
3841
3942/**
4043 * Configures the service.
4144 */
4245function getService ( ) {
43- return OAuth2 . createService ( 'Facebook' )
44- // Set the endpoint URLs.
45- . setAuthorizationBaseUrl ( 'https://www.facebook.com/dialog/oauth' )
46- . setTokenUrl ( 'https://graph.facebook.com/v2.3/oauth/access_token' )
46+ return OAuth2 . createService ( 'Facebook' )
47+ // Set the endpoint URLs.
48+ . setAuthorizationBaseUrl ( 'https://www.facebook.com/dialog/oauth' )
49+ . setTokenUrl ( 'https://graph.facebook.com/v2.3/oauth/access_token' )
4750
48- // Set the client ID and secret.
49- // You have to take care of this yourself
50- . setClientId ( PropertiesService . getScriptProperties ( ) . getProperty ( 'CLIENT_ID' ) )
51- . setClientSecret ( PropertiesService . getScriptProperties ( ) . getProperty ( 'CLIENT_SECRET' ) )
51+ // Set the client ID and secret.
52+ . setClientId ( CLIENT_ID )
53+ . setClientSecret ( CLIENT_SECRET )
5254
53- // Set the name of the callback function that should be invoked to complete
54- // the OAuth flow.
55- . setCallbackFunction ( 'authCallback' )
55+ // Set the name of the callback function that should be invoked to complete
56+ // the OAuth flow.
57+ . setCallbackFunction ( 'authCallback' )
5658
57- // Set the property store where authorized tokens should be persisted.
58- . setPropertyStore ( PropertiesService . getUserProperties ( ) ) ;
59+ // Set the property store where authorized tokens should be persisted.
60+ . setPropertyStore ( PropertiesService . getUserProperties ( ) ) ;
5961}
6062
6163/**
6264 * Handles the OAuth callback.
6365 */
6466function authCallback ( request ) {
65- var service = getService ( ) ;
66- var authorized = service . handleCallback ( request ) ;
67- if ( authorized ) {
68- return HtmlService . createHtmlOutput ( 'Success!' ) ;
69- } else {
70- return HtmlService . createHtmlOutput ( 'Denied' ) ;
71- }
67+ var service = getService ( ) ;
68+ var authorized = service . handleCallback ( request ) ;
69+ if ( authorized ) {
70+ return HtmlService . createHtmlOutput ( 'Success!' ) ;
71+ } else {
72+ return HtmlService . createHtmlOutput ( 'Denied' ) ;
73+ }
7274}
0 commit comments