5

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?

Rubén
  • 34,714
  • 9
  • 70
  • 166
Oscar Wahltinez
  • 1,155
  • 3
  • 12
  • 24
  • Can you explain this a bit more. In what service are you trying to display this? In a web app, a spreadsheet sidebar, a dialog? – Douglas Gaskell Apr 26 '16 at 10:07
  • I'm working with a web app. Embedding it into a spreadsheet add on works because users have the top bar to switch users, but outside of the GDrive context there's no way for them to know which account they are using after the initial permission dialog – Oscar Wahltinez Apr 26 '16 at 12:09

3 Answers3

2

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).

C12Z
  • 148
  • 1
  • 7
0

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).

Rubén
  • 34,714
  • 9
  • 70
  • 166
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
0

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

Weilies
  • 500
  • 1
  • 7
  • 27