Last active
August 25, 2017 19:11
-
-
Save jsdbroughton/d913e4a80517410c4e00 to your computer and use it in GitHub Desktop.
Setting and Retrieving email signatures using Google Apps Script for a Google Apps for Business/Education Domain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getSignatureById(id, domain) { | |
id = id || __userid__; | |
domain = domain || __yourdomain.com__; | |
var requestData = { | |
'method': 'GET', | |
'contentType': 'application/atom+xml' | |
}; | |
requestData['oAuthServiceName'] = 'emailsignatures'; | |
requestData['oAuthUseToken'] = 'always'; | |
UrlFetchApp.addOAuthService(signatures_oAuth()); | |
var signature = UrlFetchApp.fetch('https://apps-apis.google.com/a/feeds/emailsettings/2.0/'+domain+'/'+id+'/signature', requestData); //returns XML | |
} | |
function setSignatureById(id, domain, signature) { | |
id = id || 'userid; | |
domain = domain || 'yourdomain.com'; | |
var requestData = { | |
'method': 'PUT', | |
'contentType': 'application/atom+xml', | |
'payload': signaturePayload(signature) | |
}; | |
requestData['oAuthServiceName'] = 'emailsignatures'; | |
requestData['oAuthUseToken'] = 'always'; | |
UrlFetchApp.addOAuthService(signatures_oAuth()); | |
var signature = UrlFetchApp.fetch('https://apps-apis.google.com/a/feeds/emailsettings/2.0/'+domain+'/'+id+'/signature', requestData); //returns XML | |
} | |
function signatures_oAuth() { | |
var oAuthConfig = UrlFetchApp.addOAuthService('emailsignatures'); | |
oAuthConfig.setConsumerSecret(UserProperties.getProperty('oAuthConsumerSecret') ); | |
oAuthConfig.setConsumerKey(UserProperties.getProperty('oAuthClientID')); | |
oAuthConfig.setRequestTokenUrl('https://www.google.com/accounts/OAuthGetRequestToken?scope=https%3A%2F%2Fapps-apis.google.com%2Fa%2Ffeeds%2Femailsettings%2F'); | |
oAuthConfig.setAuthorizationUrl('https://www.google.com/accounts/OAuthAuthorizeToken'); | |
oAuthConfig.setAccessTokenUrl('https://www.google.com/accounts/OAuthGetAccessToken'); | |
return oAuthConfig; | |
} | |
function signaturePayload(signature) { | |
signature = signature.replace(/&/g, '&').replace(/</g, '<'); | |
signature = signature.replace(/>/g, '>').replace(/'/g, ''').replace(/"/g, '"'); | |
var xml = '<?xml version="1.0" encoding="utf-8"?>' + | |
'<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" xmlns:apps="http://schemas.google.com/apps/2006" >' + | |
'<apps:property name="signature" value="' + signature + '" /></atom:entry>'; | |
return xml; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment