How to invoke a gmail service from google site - google-apps-script

I read the tutorial, https://developers.google.com/apps-script/articles/building-sites-app-part2 that uses spreadsheet service in Google sites. The code uses the spreadsheet key. However to access my mail should I use Oauth or something like,
var email = String(Session.getUser().getUserLoginId());
Can we also use minute trigger when we embed GAS in google sites?

Your question is not clear. What do you want to do ? Do you want to get the email ID of the user or do you want to access your email.
Session.getUser().getUserLoginId() only returns the login id of the logged in user. And it works only in Google Apps accounts (not consumer accounts).
If you want to access your email, then make use of the Gmail service

Related

How do you automatically send an email on Google form submit, from the person who submitted the form and not the Google form owner?

After someone (authorised and not myself) submits the Google form I created, which sends an email out to whoever they want, it shows that I (the Google form owner) sent it out myself. Is there a way to remove my email and show the email of the person who submitted the form?
This is the code in Apps Script
function onFormSubmit(e) {
let responses = e.namedValues;
MailApp.sendEmail({
to: responses['Email'],
subject: responses['Subject'],
htmlBody: responses['Message'],
});
}
I believe it might involve using Google Cloud Projects and an API, but I have little/no experience with those.
The problem occurs because your script is using an installable trigger and the Google Apps Script Mail service, as the script is ran using the credentials of the user who create the trigger, in this case you.
In order to send an email using the form submitter email address as sender, one option is to use
Google Workspace accounts from the same domain
Service account
Domain wide delegation of authority
There might be other options to send the emails as the form submitter but all that have thinking about using Google Forms looks to be cumberstone.
A more simple option, still using Google Apps Script, is to create a web app and set it as run as the user accesing the web app.
The more simple solution to avoid showing your email address as the sender is to change the approach: use an alias with a generic name. To do this add an alias to your Gmail account, use GmailApp instead of MailApp and set the script to use the alias instead of your email address.

How to get active user's email in apps script?

So I have a company website which has different permissions for different users. The authentication works by comparing the current users google login email to the ones on a google sheet, and that way gets the correct permissions for the website. The program works well, but can't detect the email from an user who isn't the editor of the script. I understand this is to prevent websites collecting email adresses, but is there a way to ask the user's permission to view his/hers login email adress name in order to get the correct permissions?
The code looks like this:
var user = Session.getActiveUser();
var email = user.getEmail();
var sheet = SpreadsheetApp.openByUrl('link to the google sheet I use').getActiveSheet();
...
//In a loop to go through sheet:
if(sheet.getRange(i,1).getValue() === email && email !== ""){
//Continue...
}
If i try this with other users, the email is blank. Any idea what I could do?
According to the Apps Script Web App documentation here:
The permissions for a web app differ depending how you choose to execute the app:
Execute the app as me — In this case, the script always executes as you, the owner of the script, no matter who accesses the web app.
Execute the app as user accessing the web app — In this case, the script runs under the identity of the active user using the web app. This permission approach causes the web app to show the email of the script owner when the user authorizes access.
Therefore, in order to retrieve the email of the current user you will have to change the permission to Execute the app as user accessing the web app.
Reference
Apps Script Web Apps.

How to change email signature via google form with data from google directory

I am using a Google Apps Script to change the signature of each user with personal data which is accessed via Directory API. Therefore I have a Service account with Domain Wide Delegation, which impersonates with an admin account to get all the users and their data and then impersonates which each and every user to change the signature via GmailAPI.
However now in addition I want to provide a Google Form for each user where the user can add additional information to its signature and set it. The script will need the users information from the Directory but I was wondering if the same approach with impersonating is needed.
With The Apps Script advanced Gmail Service it is possible to set the signature but I did not find a way to access the users data in the Google Directory of the specific user which is sending the form.
If I a service account is needed for that I was wondering where to store the access credentials for that? I don't want is be accessible by every user.

Google apps script authorisation by admin for all users of a domain

I have a timesheet application for employees of my company. I am not the admin. The application is a google apps script which takes the time sheet inputs and then writes them to a spreadsheet on my google drive. When I try to publish the app to users to run under their own login (company domain maintained by google mail), it states that "Users will be asked to authorize the script." Which means I have to give them the right to view the script. I only want them to use the url and see the pages rendered by the script.
Can the admin of the domain authorise so that all users using the email for the company can login using the email and use the script.
Do I need to transfer the script and the spreadsheet to someone with admin rights or can I continue to keep both on my drive and have admin authorise.
Regards
Which means I have to give them the right to view the script.
No, it doesn't.
I only want them to use the url and see the pages rendered by the script.
That's what they will see.
Can the admin of the domain authorise so that all users using the email for the company can login using the email and use the script.
No they can't because you set your webapp to run under the users login. One alternative is to create a service account which could be authorized by the domain-administrator to run domain-wide delegation of authority.
Do I need to transfer the script and the spreadsheet to someone with admin rights or can I continue to keep both on my drive and have admin authorise.
As explained previously, there is no need to have an admin authorize to keep your script code "secret".

Receive Google user's id token in Google App Script from backend server

I am trying to create a backend service that helps track change in user's google sheet timely and send notificatioin.
From the Google Apps Docs, I can access the user's active sheet and cell, which helps my service recognize which content in the google sheet to track.
From the Google Sign-In Docs, I can access the user's google sheet in order to retrieve the sheet's content and therefore know the changes to notify.
However I am not sure how to retrieve the user's id token, since it is not recommended to directly send the user's id to backend so as to prevent malicious requests to my server using fake user id.
One way I can think of is to integrate google sign-in to the Google App script, but I don't think it will be optimal?