| 
 | 1 | +/**  | 
 | 2 | + * Saleforce's Auth flow https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/quickstart_oauth.htm  | 
 | 3 | + */  | 
 | 4 | + | 
 | 5 | +var CLIENT_ID = '...';  | 
 | 6 | +var CLIENT_SECRET = '...';  | 
 | 7 | + | 
 | 8 | +/**  | 
 | 9 | + * Authorizes and makes a request to the Saleforce API.  | 
 | 10 | + */  | 
 | 11 | +function run() {  | 
 | 12 | + var service = getService();  | 
 | 13 | + if (service.hasAccess()) {  | 
 | 14 | + // GET requests require access_token parameter  | 
 | 15 | + | 
 | 16 | + var response = UrlFetchApp.fetch(service.getToken().instance_url + '/services/data/v24.0/chatter/users/me', {  | 
 | 17 | + headers: {  | 
 | 18 | + Authorization: 'Bearer ' + service.getAccessToken()  | 
 | 19 | + }  | 
 | 20 | + });  | 
 | 21 | + var result = JSON.parse(response.getContentText());  | 
 | 22 | + Logger.log(JSON.stringify(result, null, ' '));  | 
 | 23 | + } else {  | 
 | 24 | + var authorizationUrl = service.getAuthorizationUrl();  | 
 | 25 | + Logger.log('Open the following URL and re-run the script: %s',  | 
 | 26 | + authorizationUrl);  | 
 | 27 | + }  | 
 | 28 | +}  | 
 | 29 | + | 
 | 30 | +/**  | 
 | 31 | + * Reset the authorization state, so that it can be re-tested.  | 
 | 32 | + */  | 
 | 33 | +function reset() {  | 
 | 34 | + var service = getService();  | 
 | 35 | + service.reset();  | 
 | 36 | +}  | 
 | 37 | + | 
 | 38 | +/**  | 
 | 39 | + * Configures the service.  | 
 | 40 | + */  | 
 | 41 | +function getService() {  | 
 | 42 | + return OAuth2.createService('Saleforce')  | 
 | 43 | + // Set the endpoint URLs.  | 
 | 44 | + .setAuthorizationBaseUrl('https://login.salesforce.com/services/oauth2/authorize')  | 
 | 45 | + .setTokenUrl('https://login.salesforce.com/services/oauth2/token')  | 
 | 46 | + | 
 | 47 | + // Set the client ID and secret.  | 
 | 48 | + .setClientId(CLIENT_ID)  | 
 | 49 | + .setClientSecret(CLIENT_SECRET)  | 
 | 50 | + | 
 | 51 | + // Set the name of the callback function that should be invoked to complete  | 
 | 52 | + // the OAuth flow.  | 
 | 53 | + .setCallbackFunction('authCallback')  | 
 | 54 | + | 
 | 55 | + // Set the property store where authorized tokens should be persisted.  | 
 | 56 | + .setPropertyStore(PropertiesService.getUserProperties());  | 
 | 57 | +}  | 
 | 58 | + | 
 | 59 | +/**  | 
 | 60 | + * Handles the OAuth callback.  | 
 | 61 | + */  | 
 | 62 | +function authCallback(request) {  | 
 | 63 | + var service = getService();  | 
 | 64 | + var authorized = service.handleCallback(request);  | 
 | 65 | + if (authorized) {  | 
 | 66 | + return HtmlService.createHtmlOutput('Success!');  | 
 | 67 | + } else {  | 
 | 68 | + return HtmlService.createHtmlOutput('Denied');  | 
 | 69 | + }  | 
 | 70 | +}  | 
 | 71 | + | 
 | 72 | +/**  | 
 | 73 | + * Logs the redict URI to register in the Saleforce Apps Page.  | 
 | 74 | + */  | 
 | 75 | +function logRedirectUri() {  | 
 | 76 | + var service = getService();  | 
 | 77 | + Logger.log(service.getRedirectUri());  | 
 | 78 | +}  | 
0 commit comments