4
4
* https://developers.facebook.com/apps/
5
5
*/
6
6
7
+ var CLIENT_ID = '...' ;
8
+ var CLIENT_SECRET = '...' ;
9
+
7
10
/*
8
11
* Authorizes and makes a request to the Facebook API.
9
12
*/
10
13
11
14
function 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
+ }
29
32
}
30
33
31
34
/**
32
35
* Reset the authorization state, so that it can be re-tested.
33
36
*/
34
37
function reset ( ) {
35
- var service = getService ( ) ;
36
- service . reset ( ) ;
38
+ var service = getService ( ) ;
39
+ service . reset ( ) ;
37
40
}
38
41
39
42
/**
40
43
* Configures the service.
41
44
*/
42
45
function 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' )
47
50
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 )
52
54
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' )
56
58
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 ( ) ) ;
59
61
}
60
62
61
63
/**
62
64
* Handles the OAuth callback.
63
65
*/
64
66
function 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
+ }
72
74
}
0 commit comments