Created
April 12, 2017 21:39
-
-
Save cjs/e40590f52f19c5bcd78adbb79d276b52 to your computer and use it in GitHub Desktop.
A sample Google Apps Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Authorizes and makes a request to the GitHub API. | |
*/ | |
function run() { | |
var service = getService(); | |
if (service.hasAccess()) { | |
var url = 'https://cjs.sickbaylab.com/api/v3/user'; | |
var response = UrlFetchApp.fetch(url, { | |
headers: { | |
Authorization: 'Bearer ' + service.getAccessToken() | |
} | |
}); | |
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() { | |
var service = getService(); | |
service.reset(); | |
} | |
/** | |
* Configures the service. | |
*/ | |
function getService() { | |
return OAuth2.createService('GitHub') | |
// Set the endpoint URLs. | |
.setAuthorizationBaseUrl('https://cjs.sickbaylab.com/login/oauth/authorize') | |
.setTokenUrl('https://cjs.sickbaylab.com/login/oauth/access_token') | |
// Set the client ID and secret. | |
.setClientId(PropertiesService.getScriptProperties().getProperty('clientID')) | |
.setClientSecret(PropertiesService.getScriptProperties().getProperty('clientSecret')) | |
// 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()) | |
} | |
/** | |
* 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'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment