How to Use the Box API with Google Apps Script

You can use the OAuth2 library with Google Apps script to connect to the Box API. The Box API is available to all users and provide full access (read and write) to your Box account.

To get started, create a new Box application at developers.box.com and set the redirect_uri to your Google Apps Script project. Next include the OAuth2 library in your Google Script and call authorizeBox() from the Run menu to authorize the connection between your Google Account and Box account.

The getFolderList method will logs a list of all folders that are in the root folder of your Box account.

// Written by Amit Agarwal www.ctrlq.org

// Step 1.
function authorizeBox() {
  var service = getBoxService_();
  if (!service.hasAccess()) {
    var authorizationUrl = service.getAuthorizationUrl();
    Logger.log('Open the following URL to authorize: %s', authorizationUrl);
  } else {
    Logger.log('Your account is already authorized');
  }
}

// Step 2.
function getFoldersList() {
  var response = UrlFetchApp.fetch('https://api.box.com/2.0/folders/0/items?fields=name,type', {
    headers: {
      Authorization: 'Bearer ' + getBoxService_().getAccessToken(),
    },
  });

  var result = JSON.parse(response.getContentText());
  var items = result.entries;

  var folders = [];

  for (var i = 0; i < items.length; i++) {
    if (items[i].type === 'folder') {
      folders.push({ name: items[i].name, id: items[i].id });
    }
  }

  Logger.log(folders);
}

/**
 * Configures the service.
 */
function getBoxService_() {
  return OAuth2.createService('Box')
    .setAuthorizationBaseUrl('https://app.box.com/api/oauth2/authorize')
    .setTokenUrl('https://app.box.com/api/oauth2/token')
    .setClientId(CLIENT_ID)
    .setClientSecret(CLIENT_SECRET)
    .setCallbackFunction('authCallback')
    .setPropertyStore(PropertiesService.getUserProperties());
}

/**
 * Handles the OAuth callback.
 */
function authCallback(request) {
  var service = getBoxService_();
  var authorized = service.handleCallback(request);
  if (authorized) {
    return HtmlService.createHtmlOutput('Your Google account is now connected to Box');
  } else {
    return HtmlService.createHtmlOutput('Sorry, the connection to Box was denied');
  }
}
Amit Agarwal

Amit Agarwal

Google Developer Expert, Google Cloud Champion

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards in 2017.

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

Sign up for our email newsletter to stay up to date.

We will never send any spam emails. Promise.