I am using POST https://www.googleapis.com/gmail/v1/users/userId/settings/sendAs and able to add alias id with a signature in Gsuite account however with same code I am trying to update the signature for primary Gsuite account but it's not updating.
Please guide how to apply a logic to update the primary Gsuite account signature using a service account
https://developers.google.com/gmail/api/v1/reference/users/settings/sendAs
var service_account = {
"private_key":"-----BEGIN PRIVATE KEY-----VE=\n-----END PRIVATE KEY-----\n",
"client_email":"xxxxxx",
"client_id": "xxxxxxx",
"userEmail" = '[email protected]';
};
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(userEmail)
.setPropertyStore(PropertiesService.getScriptProperties())
.setParam('access_type', 'offline')
.setParam('approval_prompt', 'force')
.setScope('https://www.googleapis.com/auth/gmail.settings.sharing https://www.googleapis.com/auth/gmail.settings.basic');
}
function createAlias() {
var userEmail = '[email protected]';
//var alias = '[email protected]';
//var alias_name = ' User';
var signature = 'Testing';
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,
signature: signature,
replyToAddress : alias,
treatAsAlias: true,
verificationStatus: 'accepted',
isPrimary:true,
isDefault:true
};
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();
}