Skip to content

Commit 2469486

Browse files
author
Eric Koleda
committed
Merge pull request #40 from oshliaer/facebook
Facebook
2 parents aa7f519 + 5eea048 commit 2469486

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

samples/Facebook.gs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
}

samples/Yahoo.gs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var CLIENT_ID = '...';
88
var CLIENT_SECRET = '...';
99

1010
/**
11-
* Authorizes and makes a request to the LinkedIn API.
11+
* Authorizes and makes a request to the Yahoo API.
1212
*/
1313

1414
function run() {
@@ -45,15 +45,15 @@ function getService() {
4545
// Set the endpoint URLs.
4646
.setAuthorizationBaseUrl('https://api.login.yahoo.com/oauth2/request_auth')
4747
.setTokenUrl('https://api.login.yahoo.com/oauth2/get_token')
48-
48+
4949
// Set the client ID and secret.
5050
.setClientId(CLIENT_ID)
5151
.setClientSecret(CLIENT_SECRET)
52-
52+
5353
// Set the name of the callback function that should be invoked to complete
5454
// the OAuth flow.
5555
.setCallbackFunction('authCallback')
56-
56+
5757
// Set the property store where authorized tokens should be persisted.
5858
.setPropertyStore(PropertiesService.getUserProperties());
5959
}

0 commit comments

Comments
 (0)