Guest User

ClicData-GoogleScript

a guest
Sep 3rd, 2019
2,078
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * ClicData Auth flow
  3.  * @see https://app.clicdata.com/help/apidocumentation/api-getting-started
  4.  * Using library: https://github.com/gsuitedevs/apps-script-oauth2
  5.  * Resources > Libraries > Find a Library -> Enter: 1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF
  6.  */
  7. function run() {
  8.     /**
  9.      * Authorizes and makes a request to the ClicData API.
  10.      */
  11.     // Complete all pending spreadsheet actions before adding responseURLs
  12.     SpreadsheetApp.flush();
  13.     // Base Server Address
  14.     var SERVER = 'https://api.clicdata.com/';
  15.     //Schedule RecId's
  16.     var SCHEDULE_ID = 'RecId';
  17.     // Data Set RecId's
  18.     var DATA_ID = 'RecId';
  19.     // Task Options
  20.     var DATAREFRESH = '/data/' + DATA_ID + '/reload';
  21.     var RUNSCHEDULE = '/schedule/' + SCHEDULE_ID + '/trigger';
  22.     var serviceProject = getService();
  23.     Logger.log(serviceProject.hasAccess());
  24.     if (serviceProject.hasAccess()) {
  25.         var url = SERVER + DATAREFRESH; // Replace to POST different commands (see Task Options above)
  26.         Logger.log(serviceProject.getAccessToken());
  27.         // Try to load, if task is already running, wait, then try again
  28.         var runCheck = false // Set runCheck to false to initialize loop
  29.             restartloop: while (runCheck === false) {
  30.                 try {
  31.                     var response = UrlFetchApp.fetch(url, {
  32.                             "method": "POST", // Change method to GET, POST, DELETE or PUT
  33.                             headers: {
  34.                                 Authorization: 'Bearer ' + service.getAccessToken()
  35.                             }
  36.                         });
  37.                     runCheck = true // Stop looping
  38.                 } catch (e) {
  39.                     if (e.message.indexOf('data is already being processed or is queued for processing') > -1) {
  40.                         Utilities.sleep(4000); // pause between refreshes for 4 second (trying to fix error code 400)
  41.                         continue restartloop;
  42.                     } else {
  43.                         // Logs an ERROR message.
  44.                         console.error('UrlFetchApp yielded an error: ' + e);
  45.                         return; // Exits function and loop
  46.                         break;
  47.                     }
  48.                 }
  49.             }
  50.             Logger.log('Success, data set ' + DATA_ID + ' has been reloaded.') // Log Success!
  51.     } else {
  52.         Logger.log(service.getLastError());
  53.         // If Refresh Token has expired, reset authorization state then run authorization again (only required on first getService() call
  54.         reset();
  55.         run();
  56.     }
  57. }
  58. function reset() {
  59.     /**
  60.      * Reset the authorization state.
  61.      */
  62.     getService().reset();
  63. }
  64. function getService() {
  65.     /**
  66.      * Configures the service.
  67.      */
  68.     //Client_Credentials id and secret, obtain from ClicData Account settings
  69.     var CLIENT_ID = 'id';
  70.     var CLIENT_SECRET = 'secret';
  71.     return OAuth2.createService('ClicDataAdmin')
  72.     // Set the endpoint URLs.
  73.     .setTokenUrl('https://api.clicdata.com/oauth20/token')
  74.     // Set the client ID and secret.
  75.     .setClientId(CLIENT_ID)
  76.     .setClientSecret(CLIENT_SECRET)
  77.     // Sets the custom grant type to use.
  78.     .setGrantType('client_credentials')
  79.     // Set the property store where authorized tokens should be persisted.
  80.     .setPropertyStore(PropertiesService.getUserProperties());
  81. }
Add Comment
Please, Sign In to add comment