h0dges
h0dges

Reputation: 693

Switching accounts in Google Apps Script HTML Service

I'm trying to allow users of my Google Apps Script web app to switch google accounts. I've tried sending users to the Account Chooser via a hyperlink:

<a href="https://accounts.google.com/AccountChooser?continue=https://script.google.com/a/macros/[domain]/s/[site_id]/exec">Switch Accounts</a>

If the user chooses another account on that screen, the user returns to the web app still logged in under the original account.

What am I doing wrong, or is there another way of allowing users to switch accounts whilst on the web app?

Upvotes: 3

Views: 706

Answers (1)

Teyam
Teyam

Reputation: 8112

While you might be needing Google Apps Script HTML Service to serve web pages that can interact with server-side Apps Script functions for your custom user interface, you might also need Admin SDK's Directory API to manage users.

As stated in the documentation,

The Admin SDK Directory service allows you to use the Admin SDK's Directory API in Apps Script. This API gives administrators of Google Apps domains (including resellers) the ability to manage devices, groups, users, and other entities in their domains.

First, you may use users: list to list all possible users. Example, you can list users in a domain which can be sorted by first name:

function listAllUsers() {
  var pageToken, page;
  do {
    page = AdminDirectory.Users.list({
      domain: 'example.com',
      orderBy: 'givenName',
      maxResults: 100,
      pageToken: pageToken
    });
    var users = page.users;
    if (users) {
      for (var i = 0; i < users.length; i++) {
        var user = users[i];
        Logger.log('%s (%s)', user.name.fullName, user.primaryEmail);
      }
    } else {
      Logger.log('No users found.');
    }
    pageToken = page.nextPageToken;
  } while (pageToken);
}

Then, use users: get to retrieve a user.

Here's a sample code to get a user by email address and logs all of their data as a JSON string.

function getUser() {
  var userEmail = 'liz@example.com';
  var user = AdminDirectory.Users.get(userEmail);
  Logger.log('User data:\n %s', JSON.stringify(user, null, 2));
}

Furthermore, please note that there are Directory API: Prerequisites and also needs authorization as described in Authorize requests in using this API.

This may seem to be a bit confusing so I suggest that you please go through the given links for more information.

Happy coding!

Upvotes: -1

Related Questions