5

I am learning to make gmail addons. But I am stuck at a point on how to access email address value of user who using addons.

After Reading I get that I will need this permission https://www.googleapis.com/auth/userinfo.email

But how to actually get email address when user opens addon in Gmail

Please can anybody help me on this

Sandeep Chikhale
  • 1,505
  • 2
  • 21
  • 36
  • What do you need the id for? You can just use the string "me" if it's for other Apps. Otherwise, I'm not sure if you can simply get it, but I guess you can fetch it from the user's gmail threads for example. – benams Apr 29 '18 at 14:14
  • 1
    I am using email id to check whether this user registered with our system or not - so i am going to make a api call to my servers that is why - hey could you please provide example -- – Sandeep Chikhale Apr 29 '18 at 14:17
  • `Session.getActiveUser().getEmail()` You'll need AUTH.FULL for every user for this to probably work. – TheMaster Apr 29 '18 at 14:47

1 Answers1

8

This seems to work:

var emailAddress = Session.getEffectiveUser().getEmail();
Glen Little
  • 6,951
  • 4
  • 46
  • 68
  • Thanks Glen - Can you give me link from where I read more about this function and others - and know about how can I access various properties. – Sandeep Chikhale Apr 30 '18 at 06:57
  • @SandeepChikhale, you can find this one here: https://developers.google.com/apps-script/reference/base/session#getEffectiveUser() The more obvious `getActiveUser` does not seem to work in Gmail Add-ons. – Glen Little Apr 30 '18 at 14:21
  • In working with my add-on, something changed, and now `getActiveUser()` works, and `getEffectiveuser()` fails saying "You do not have permission to call getEffectiveUser"! – Glen Little May 16 '18 at 06:30
  • And adding "https://www.googleapis.com/auth/userinfo.email" back into the manifest file (appsscript.json) got them both working again. – Glen Little May 16 '18 at 06:34
  • Even with "googleapis.com/auth/userinfo.email", `getActiveUser()` does not work for me, only `getEffectiveUser()` works. Btw, I have to use `USER_ACCESSING` in the field of `executeAs` in the manifest file. – Walty Yeung Nov 30 '18 at 12:26