Skip to content

Commit 933d28d

Browse files
author
Sourabh Choraria
authored
Create Notion.gs (googleworkspace#290)
1 parent 3a32f4f commit 933d28d

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

samples/Notion.gs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
var CLIENT_ID = '...';
2+
var CLIENT_SECRET = '...';
3+
4+
/**
5+
* Authorizes and makes a request to the Notion API - https://developers.notion.com/reference/intro
6+
*/
7+
function run() {
8+
var service = getService();
9+
if (service.hasAccess()) {
10+
var url = 'https://api.notion.com/v1/search'; // refer https://developers.notion.com/reference/post-search
11+
var options = {
12+
'method': 'post',
13+
'muteHttpExceptions': true,
14+
'headers': {
15+
'Notion-Version': '2021-05-13', // refer https://developers.notion.com/reference/versioning
16+
'Authorization': 'Bearer ' + service.getAccessToken()
17+
}
18+
};
19+
var response = UrlFetchApp.fetch(url, options);
20+
var result = JSON.parse(response.getContentText());
21+
Logger.log(JSON.stringify(result, null, 2));
22+
} else {
23+
var authorizationUrl = service.getAuthorizationUrl();
24+
Logger.log('Open the following URL and re-run the script: %s', authorizationUrl);
25+
}
26+
}
27+
28+
/**
29+
* Reset the authorization state, so that it can be re-tested.
30+
*/
31+
function reset() {
32+
getService().reset();
33+
}
34+
35+
/**
36+
* Configures the service.
37+
*/
38+
function getService() {
39+
return OAuth2.createService('Notion')
40+
// Set the endpoint URLs.
41+
.setAuthorizationBaseUrl('https://api.notion.com/v1/oauth/authorize')
42+
.setTokenUrl('https://api.notion.com/v1/oauth/token')
43+
44+
// Set the client ID and secret.
45+
.setClientId(CLIENT_ID)
46+
.setClientSecret(CLIENT_SECRET)
47+
48+
// Set the name of the callback function that should be invoked to
49+
// complete the OAuth flow.
50+
.setCallbackFunction('authCallback')
51+
52+
// Set the property store where authorized tokens should be persisted.
53+
.setPropertyStore(PropertiesService.getUserProperties())
54+
55+
.setTokenHeaders({
56+
'Authorization': 'Basic ' + Utilities.base64Encode(CLIENT_ID + ':' + CLIENT_SECRET)
57+
});
58+
}
59+
60+
/**
61+
* Handles the OAuth callback.
62+
*/
63+
function authCallback(request) {
64+
var service = getService();
65+
var authorized = service.handleCallback(request);
66+
if (authorized) {
67+
return HtmlService.createHtmlOutput('Success!');
68+
} else {
69+
return HtmlService.createHtmlOutput('Denied.');
70+
}
71+
}
72+
73+
/**
74+
* Logs the redict URI to register.
75+
*/
76+
function logRedirectUri() {
77+
Logger.log(OAuth2.getRedirectUri());
78+
}

0 commit comments

Comments
 (0)