Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Figma.gs #455

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions samples/Figma.gs
Original file line number Diff line number Diff line change
@@ -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());
}
83 changes: 83 additions & 0 deletions samples/Reddit.gs
Original file line number Diff line number Diff line change
@@ -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());
}