Oscar Wahltinez
Oscar Wahltinez

Reputation: 1195

How to select account in Google App Scripts for users with multiple accounts?

For users that are logged into multiple accounts, how can a script let the user pick an account? For example, Gmail, Google Drive, etc. provide a way for the user to select which account to use with a selectable option on the top-right of the page:

gdrive screenshot

How can developers implement a similar mechanism?

Upvotes: 6

Views: 3109

Answers (3)

ReyAnthonyRenacia
ReyAnthonyRenacia

Reputation: 17613

You can try using the Directory API to work with Apps Script. Retrieve the user using:

GET https://www.googleapis.com/admin/directory/v1/users/userKey. 

You can then make an interface that displays the user accounts details(Name, email,etc).

Upvotes: 0

Weilies
Weilies

Reputation: 530

With Chrome Version 70.0.3538.102. You may resolve your issue (at least i did)

Now i make sure i ONLY signed in ONE account at a time. Then use "manage people"enter image description here if i have 3 google accounts, i will create 3 people and each time you only have 1 active google account session. With this setup, i ensured everytime my script only execute with my G Suite user instead of @gmail accounts

Upvotes: 0

C12Z
C12Z

Reputation: 148

There seems to be no way to do that with Google Apps Script libraries - GAS just uses the current primary account. Also, unfortunately, the API Client Library and thus Google Sign-In for websites don't work inside GAS web apps because of the sandbox frame. You could write your own or use some existing OAuth implementation to authenticate with Google but I found a much much simpler solution using Auth0 Lock with only a Google Connection (using the popup method because the redirect method doesn't work within the sandbox frame).

Upvotes: 2

Related Questions