-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFbSdk.gs
77 lines (60 loc) · 2.04 KB
/
FbSdk.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var FB = {
baseUrl: 'https://graph.facebook.com/',
/**
* Initializes the SDK and OAuth2 service.
* https://github.com/googlesamples/apps-script-oauth2
*/
init: function (opt) {
var url = this.baseUrl + (opt.apiVersion || '');
this.oauth = OAuth2.createService('Facebook')
.setAuthorizationBaseUrl('https://www.facebook.com/dialog/oauth')
.setTokenUrl(url+'/oauth/access_token')
.setClientId(opt.appId)
.setClientSecret(opt.appSecret)
.setCallbackFunction('FbSdkAuthCallback_')
.setPropertyStore(PropertiesService.getUserProperties());
},
/**
* Authorizes and makes a request to the Facebook API.
*/
api: function (endpoint) {
this.previousEndpointCall = endpoint;
var resp = UrlFetchApp.fetch(this.baseUrl+endpoint, {
headers: {
'Authorization': 'Bearer ' + this.oauth.getAccessToken()
}
});
return JSON.parse(resp.getContentText());
},
/**
* Generates an authorization URL to ask a user for access to their profile on behalf of your app.
*/
getLoginUrl: function (permissions) {
var scopes = permissions || [
'pages_show_list', // For displaying a list of Pages
'pages_manage_instant_articles', // For accessing the `supports_instant_articles` field of `/accounts` endpoint to determine if Page has signed up for IA
'read_insights' // For getting the actual insights of the Page/article
];
return this.oauth.getAuthorizationUrl()
+'&scope='+scopes.join(',');
}
};
/**
* Handles the OAuth callback.
*/
function FbSdkAuthCallback_(request) {
var isAuthorized = FB.oauth.handleCallback(request);
if (isAuthorized) {
// update sidebar
//SpreadsheetApp.getUi().showSidebar(page);
//ContentService.createTextOutput('Success!')
return HtmlService.createHtmlOutput('Success! You may close this tab.');
} else {
return HtmlService.createHtmlOutput('Denied. You may close this tab.');
}
};
FB.init({
appId: '165010617304976',
appSecret: APP_SECRET,
// version: 'v2.8'
});