From 5c7905afc6f6c5b57459421d113d18a95a042f5c Mon Sep 17 00:00:00 2001 From: Sourabh Choraria Date: Mon, 31 Jul 2023 12:18:37 +0530 Subject: [PATCH 1/2] Create Figma.gs --- samples/Figma.gs | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 samples/Figma.gs diff --git a/samples/Figma.gs b/samples/Figma.gs new file mode 100644 index 00000000..68047cda --- /dev/null +++ b/samples/Figma.gs @@ -0,0 +1,78 @@ +var CLIENT_ID = '...'; +var CLIENT_SECRET = '...'; + +/** + * Authorizes and makes a request to the Figma API - https://www.figma.com/developers/api#authentication + */ +function run() { + var service = getService_(); + if (service.hasAccess()) { + var url = 'https://api.figma.com/v1/me'; // refer https://www.figma.com/developers/api#get-me-endpoint + var options = { + 'method': 'GET', + 'muteHttpExceptions': true, + 'headers': { + 'Authorization': 'Bearer ' + service.getAccessToken() + } + }; + var response = UrlFetchApp.fetch(url, options); + var result = JSON.parse(response.getContentText()); + Logger.log(JSON.stringify(result, null, 2)); + } else { + var authorizationUrl = service.getAuthorizationUrl(); + Logger.log('Open the following URL and re-run the script: %s', + authorizationUrl); + } +} + +/** + * Reset the authorization state, so that it can be re-tested. + */ +function reset() { + getService_().reset(); +} + +/** + * Configures the service. + */ +function getService_() { + return OAuth2.createService('Figma') + // Set the endpoint URLs. + .setAuthorizationBaseUrl('https://www.figma.com/oauth') + .setTokenUrl('https://www.figma.com/api/oauth/token') + .setRefreshUrl('https://www.figma.com/api/oauth/refresh') + + // Set the client ID and secret. + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + + // Set the name of the callback function that should be invoked to + // complete the OAuth flow. + .setCallbackFunction('authCallback') + + // Set the property store where authorized tokens should be persisted. + .setPropertyStore(PropertiesService.getUserProperties()) + + // Set the scopes to request + .setScope('files:read,file_variables:read,file_dev_resources:read') +} + +/** + * Handles the OAuth callback. + */ +function authCallback(request) { + var service = getService_(); + var authorized = service.handleCallback(request); + if (authorized) { + return HtmlService.createHtmlOutput('Success!'); + } else { + return HtmlService.createHtmlOutput('Denied.'); + } +} + +/** + * Logs the redict URI to register. + */ +function logRedirectUri() { + Logger.log(OAuth2.getRedirectUri()); +} From c0139189c8a0f0af3fff598c8c5741aa8d21861b Mon Sep 17 00:00:00 2001 From: Sourabh Choraria Date: Thu, 7 Sep 2023 11:02:44 +0530 Subject: [PATCH 2/2] Create Reddit.gs Adding Reddit to the list of samples. --- samples/Reddit.gs | 83 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 samples/Reddit.gs diff --git a/samples/Reddit.gs b/samples/Reddit.gs new file mode 100644 index 00000000..c8a7cee2 --- /dev/null +++ b/samples/Reddit.gs @@ -0,0 +1,83 @@ +var CLIENT_ID = '...'; +var CLIENT_SECRET = '...'; + +/** + * Authorizes and makes a request to the Reddit API - https://www.reddit.com/dev/api + */ +function run() { + var service = getService_(); + if (service.hasAccess()) { + var url = 'https://oauth.reddit.com/api/v1/me'; // refer https://www.reddit.com/dev/api#GET_api_v1_me + var options = { + 'method': 'GET', + 'muteHttpExceptions': true, + 'headers': { + 'Authorization': 'Bearer ' + service.getAccessToken() + } + }; + var response = UrlFetchApp.fetch(url, options); + var result = JSON.parse(response.getContentText()); + Logger.log(JSON.stringify(result, null, 2)); + } else { + var authorizationUrl = service.getAuthorizationUrl(); + Logger.log('Open the following URL and re-run the script: %s', + authorizationUrl); + } +} + +/** + * Reset the authorization state, so that it can be re-tested. + */ +function reset() { + getService_().reset(); +} + +/** + * Configures the service. + */ +function getService_() { + return OAuth2.createService('Reddit') + // Set the endpoint URLs. + .setAuthorizationBaseUrl('https://www.reddit.com/api/v1/authorize') + .setTokenUrl('https://www.reddit.com/api/v1/access_token') + + // Set the client ID and secret. + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + + // Set the name of the callback function that should be invoked to + // complete the OAuth flow. + .setCallbackFunction('authCallback') + + // Set the property store where authorized tokens should be persisted. + .setPropertyStore(PropertiesService.getUserProperties()) + + .setTokenHeaders({ + 'Authorization': 'Basic ' + + Utilities.base64Encode(CLIENT_ID + ':' + CLIENT_SECRET) + }) + + .setParam('duration', 'permanent') + + .setScope('identity read wikiread') +} + +/** + * Handles the OAuth callback. + */ +function authCallback(request) { + var service = getService_(); + var authorized = service.handleCallback(request); + if (authorized) { + return HtmlService.createHtmlOutput('Success!'); + } else { + return HtmlService.createHtmlOutput('Denied.'); + } +} + +/** + * Logs the redict URI to register. + */ +function logRedirectUri() { + Logger.log(OAuth2.getRedirectUri()); +}