Skip to content

Instantly share code, notes, and snippets.

@cdeckert
Created June 14, 2017 12:58
Show Gist options
  • Save cdeckert/5536d6c31d1fd199f88e11cf4183aaa5 to your computer and use it in GitHub Desktop.
Save cdeckert/5536d6c31d1fd199f88e11cf4183aaa5 to your computer and use it in GitHub Desktop.

Revisions

  1. cdeckert renamed this gist Jun 14, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. cdeckert created this gist Jun 14, 2017.
    143 changes: 143 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,143 @@

    ////oAuth related code

    //hardcoded here for easily tweaking this. should move this to ScriptProperties or better parameterize them
    //step 1. we can actually start directly here if that is necessary
    var AUTHORIZE_URL = '/services/oauth2/authorize';



    //step 2. after we get the callback, go get token
    var TOKEN_URL = '/services/oauth2/token';

    //PUT YOUR OWN SETTINGS HERE
    var CLIENT_ID = 'XXXXX';
    var CLIENT_SECRET='XXXXXX';

    //var getRedirectURL()= ScriptApp.getService().getUrl();

    function getRedirectURL()
    {
    return ScriptApp.getService().getUrl();
    }


    //this is the user propety where we'll store the token, make sure this is unique across all user properties across all scripts
    var tokenPropertyName = 'SALESFORCE_OAUTH_TOKEN';
    var baseURLPropertyName = 'SALESFORCE_INSTANCE_URL';


    function getBaseURL()
    {
    return PropertiesService.getUserProperties().getProperty("baseURL");
    }


    //this is the URL where they'll authorize with salesforce.com
    //may need to add a "scope" param here. like &scope=full for salesforce
    function getURLForAuthorization(){
    return getBaseURL()+AUTHORIZE_URL + '?response_type=code&client_id='+CLIENT_ID+'&redirect_uri='+getRedirectURL()+"&prompt=login%20consent";
    }

    function getAndStoreAccessToken(code){
    var nextURL = getBaseURL()+TOKEN_URL + '?client_id='+CLIENT_ID+'&client_secret='+CLIENT_SECRET+'&grant_type=authorization_code&redirect_uri='+getRedirectURL()+'&code=' + code;

    var response = UrlFetchApp.fetch(nextURL).getContentText();
    var tokenResponse = JSON.parse(response);

    //salesforce requires you to call against the instance URL that is against the token (eg. https://na9.salesforce.com/)
    PropertiesService.getUserProperties().setProperty(baseURLPropertyName, tokenResponse.instance_url);
    //store the token for later retrival
    PropertiesService.getUserProperties().setProperty(tokenPropertyName, tokenResponse.access_token);
    }


    function getUrlFetchOptions() {
    var token = PropertiesService.getUserProperties().getProperty(tokenPropertyName);
    return {
    "contentType" : "application/json",
    "headers" : {
    "Authorization" : "Bearer " + token,
    "Accept" : "application/json"
    }
    };
    }



    function getUrlFetchPOSTOptions(payload){
    var token = PropertiesService.getUserProperties().getProperty(tokenPropertyName);
    return {
    "method": "post",
    "contentType" : "application/json",
    "payload" : payload,
    "headers" : {
    "Authorization" : "Bearer " + token
    }
    }
    }

    function getUrlFetchPATCHOptions(payload){
    var token = PropertiesService.getUserProperties().getProperty(tokenPropertyName);
    return {
    "method": "patch",
    "contentType" : "application/json",
    "payload" : payload,
    "headers" : {
    "Authorization" : "Bearer " + token
    }
    }
    }

    function isTokenValid() {
    var token = PropertiesService.getUserProperties().getProperty(tokenPropertyName);
    if(!token){ //if its empty or undefined
    return false;
    }
    return true; //naive check
    }



    function doGet(e) {
    var HTMLToOutput;
    if (e.parameters.code) { //if we get "code" as a parameter in, then this is a callback. we can make this more explicit
    getAndStoreAccessToken(e.parameters.code);
    HTMLToOutput = '<html><h1>Finished with oAuth</h1>You can close this window.</html>';
    }
    return HtmlService.createHtmlOutput(HTMLToOutput);
    }

    function loginProduction(){
    PropertiesService.getUserProperties().setProperty("baseURL", "https://login.salesforce.com");
    login();


    }

    function loginSandbox(){
    PropertiesService.getUserProperties().setProperty("baseURL", "https://test.salesforce.com");
    login();


    }

    function login()
    {
    logout();
    if(isTokenValid()){

    }
    else {//we are starting from scratch or resetting
    HTMLToOutput = "<html><h1>You need to login</h1><a target='_blank' href='"+getURLForAuthorization()+"'>click here to start</a><br>Re-open this window when you return.</html>";
    SpreadsheetApp.getActiveSpreadsheet().show(HtmlService.createHtmlOutput(HTMLToOutput));
    }
    }

    function logout()
    {
    //salesforce requires you to call against the instance URL that is against the token (eg. https://na9.salesforce.com/)
    PropertiesService.getUserProperties().deleteProperty(baseURLPropertyName);
    //store the token for later retrival
    PropertiesService.getUserProperties().deleteProperty(tokenPropertyName);
    }