1+ /*
2+ * Facebook oAuth 2.0 guide API requests
3+ * https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow
4+ * https://developers.facebook.com/apps/
5+ */
6+
7+ var CLIENT_ID = '...' ;
8+ var CLIENT_SECRET = '...' ;
9+
10+ /*
11+ * Authorizes and makes a request to the Facebook API.
12+ */
13+
14+ function run ( e ) {
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+ }
32+ }
33+
34+ /**
35+ * Reset the authorization state, so that it can be re-tested.
36+ */
37+ function reset ( ) {
38+ var service = getService ( ) ;
39+ service . reset ( ) ;
40+ }
41+
42+ /**
43+ * Configures the service.
44+ */
45+ function getService ( ) {
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' )
50+
51+ // Set the client ID and secret.
52+ . setClientId ( CLIENT_ID )
53+ . setClientSecret ( CLIENT_SECRET )
54+
55+ // Set the name of the callback function that should be invoked to complete
56+ // the OAuth flow.
57+ . setCallbackFunction ( 'authCallback' )
58+
59+ // Set the property store where authorized tokens should be persisted.
60+ . setPropertyStore ( PropertiesService . getUserProperties ( ) ) ;
61+ }
62+
63+ /**
64+ * Handles the OAuth callback.
65+ */
66+ function authCallback ( request ) {
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+ }
74+ }
0 commit comments