Skip to content

Commit 6f5ea84

Browse files
committed
Facebook.gs
1 parent aa7f519 commit 6f5ea84

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

samples/Facebook.gs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
/*
8+
* Authorizes and makes a request to the Facebook API.
9+
*/
10+
11+
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);
29+
}
30+
31+
/**
32+
* Reset the authorization state, so that it can be re-tested.
33+
*/
34+
function reset() {
35+
var service = getService();
36+
service.reset();
37+
}
38+
39+
/**
40+
* Configures the service.
41+
*/
42+
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')
47+
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'))
52+
53+
// Set the name of the callback function that should be invoked to complete
54+
// the OAuth flow.
55+
.setCallbackFunction('authCallback')
56+
57+
// Set the property store where authorized tokens should be persisted.
58+
.setPropertyStore(PropertiesService.getUserProperties());
59+
}
60+
61+
/**
62+
* Handles the OAuth callback.
63+
*/
64+
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+
}
72+
}

0 commit comments

Comments
 (0)