Google Admin SDK – 500 Error Troubleshooting

google-apps-scriptgoogle-sites

I'm using the Admin SDK with Google Apps Script to create a directory of user's names and emails on a Google Site. The code seems to work fine because when I view the logs I see the results.

[14-02-04 12:24:05:996 GMT] Joe Ardee (joe.ardee@example.com)
[14-02-04 12:24:05:997 GMT] The Headmaster (jim.dix@example.com)
[14-02-04 12:24:05:997 GMT] Edward Smith(edward.smith@example.com)

After I publish it as a web app and add it to my page using the insert scripts function I get a 500 error, on the page I get a message saying Google Drive encountered an error. When I published the app I set it so that only I can access it. I have also enabled the API and enable API access in the admin console.

Here is the code I am using;

function listAllUsers() {
  var users = AdminDirectory.Users.list({domain: 'example.com'}).users;
  if(users.length != 0) {
    for (var i=0; i<users.length; i++) {
      var user = users[i];
      Logger.log('%s (%s)', user.name.fullName, user.primaryEmail, user.phones);
    }
  } else {
    Logger.log('No users found.');
  }
}

(Instead of example.com in domain I have used the correct domain)

Best Answer

That's because it needs a whole different approach. Use the following code.

Code

function doGet(e) {
  var app = UiApp.createApplication();
  var flex = app.createFlexTable();  

  var users = AdminDirectory.Users.list({domain: 'jacobjantuinstra.nl'}).users;
  if(users.length != 0) {
    for (var i=0; i<users.length; i++) {
      var user = users[i];
      flex.setWidget(i, 0, app.createLabel(user.name.fullName))
      flex.setWidget(i, 1, app.createLabel(user.primaryEmail));
    }
  } else {
    flex.setWidget(0, 0, app.createLabel('No users found.'));
  }
  app.add(flex);

  return app;
}