I do apologise for asking this here as there is already an existing thread but I was unsure of the correct protocol.
I need to use apps script to add a custom sendas address for my users in Gmail. The thread here appears to do what I need: Creates a custom "from" send-as alias with GAS and APIs.
However I am new to Apps Script (especially advanced APIs) and am unsure which sections of Jay's script that I would need to update to make this work for me.
I know that I will need to update:
function createAlias() {
var userEmail = 'useraccount@example.com';
var alias = 'myalias@example.com';
var alias_name = 'Alias User';
But I am unsure what to update the following with or where to find it:
var service_account = {
"private_key": "-----BEGIN PRIVATE KEY...",
"client_email": "sa-email@example.com",
"client_id": "1234569343",
"user_email": "useraccount@example.com"
};
I was unable to comment on the existing post and it didn't seem appropriate to add my question as an answer. For convenience, I have pasted Jay's code here.
If anyone could please let me know which variables I will need to update with my specific information (and if necessary, where to find it) that would be much appreciated.
Kind Regards,
Brett
var service_account = {
"private_key": "-----BEGIN PRIVATE KEY...",
"client_email": "sa-email@example.com",
"client_id": "1234569343",
"user_email": "useraccount@example.com"
};
function getOAuthService(user) {
return OAuth2.createService('Service Account')
.setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')
.setTokenUrl('https://accounts.google.com/o/oauth2/token')
.setPrivateKey(service_account.private_key)
.setIssuer(service_account.client_email)
.setSubject(service_account.user_email)
.setPropertyStore(PropertiesService.getScriptProperties())
.setScope('https://www.googleapis.com/auth/gmail.settings.sharing
https://www.googleapis.com/auth/gmail.settings.basic')
}
function createAlias() {
var userEmail = 'useraccount@example.com';
var alias = 'myalias@example.com';
var alias_name = 'Alias User';
var service = getOAuthService();
service.reset();
if (service.hasAccess()) {
var url = 'https://www.googleapis.com/gmail/v1/users/me/settings/sendAs'
var headers ={
"Authorization": 'Bearer ' + service.getAccessToken(),
"Accept":"application/json",
"Content-Type":"application/json",
};
var resource ={
'sendAsEmail': alias,
'displayName': alias_name
};
var options = {
'headers': headers,
'method': 'POST',
'payload': JSON.stringify(resource),
'muteHttpExceptions': true
};
Logger.log(options);
var response = UrlFetchApp.fetch(url, options);
Logger.log(response.getContentText());
}
}
function reset() {
var service = getOAuthService();
service.reset();