Google Apps Script and Netlify Rest API

Hey gang,

I’m trying to prove a site can be deployed through netlify Rest API/google apps script, as long as there’s oAuth2 working.

So far can’t do it; “Redirect URI doesn’t match application”.

What’s wrong with my code? Searched literally everywhere online.

var CLIENT_ID = 'client id';
var CLIENT_SECRET = 'mysecret';


function run() {
  var zipLink = "@https://drive.google.com/uc?export=download&id=1_fOKfBIsandsomeothertext"
  var service = getService();
  if (service.hasAccess()) {
    var url = "https://api.netlify.com/api/v1/sites";
    var options = {
      'method': 'post',
      'muteHttpExceptions': true,
      "data-binary": zipLink,
      'headers': {
        'Authorization': 'Bearer ' + service.getAccessToken(),
        "Content-Type": "application/zip"
      }
    };
    var response = UrlFetchApp.fetch(url, options);
    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('Netlify')
      // Set the endpoint URLs.
      .setAuthorizationBaseUrl('https://app.netlify.com/authorize')
      .setTokenUrl('https://api.netlify.com/auth/done')

      // 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())

      .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(OAuth2.getRedirectUri());
}

Hi @Daniel_Williamson,

All Netlify OAuth apps need to have a Redirect URL and the Redirect URL needs to be specified in the URL making the OAuth call. For example, this is what a OAuth request URL looks like:

https://app.netlify.com/authorize?client_id=<id>&response_type=token&redirect_uri=<url>&state=<state>

state is used to prevent CSRF attacks. Only when the redirect URL mentioned here and in the app settings match, the OAuth would work.

Is that what’s happening in your case?