Authenticate with OAuth 2.0, Blue Account, Apps Scripts

0

I'm having a hard time completing the Authenticating process with OAuth 2.0, I found some templates, but I still could not succeed, that's Api link , this is template I need link , because he has to request the authorization and generates the access code to continue the process, and to do the other phases.

All credentials are created, I am stopped at the stage that asks to authorize in the log, I already tried several ways to open a window that the user can click and authorize and save the return url code but I could not, that would be the library link , is already enabled in the project. '

var CLIENT_ID = 'xxxxxxxxxx';
var CLIENT_SECRET = 'xxxxxxxxx';

/**
* Authorizes and makes a request to the FitBit API.
*/
function run() {
    var service = getService();
    if (service.hasAccess()) {
        var url = 'https://api.contaazul.com/v1/sales';
        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() {
    getService().reset();
}

/**
* Configures the service.
*/
function getService() {
    return OAuth2.createService('ContaAzul')
    // Set the endpoint URLs.      
 .setAuthorizationBaseUrl('https://api.contaazul.com/auth/authorize?')
 .setTokenUrl('https://api.contaazul.com/oauth2/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())

  // Set the scope and additional headers required by the FitBit API.
  .setScope('sales')
  .setTokenHeaders({
    'Authorization': 'Basic ' +
        Utilities.base64Encode(CLIENT_ID + ':' + CLIENT_SECRET)
  });
}

/**
* 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(getService().getRedirectUri());
 }
    
asked by anonymous 30.10.2018 / 17:17

0 answers