电子邮件设置 API 身份验证

标签 email google-apps-script google-oauth settings google-workspace

我想使用电子邮件设置 API 和 Apps 脚本来管理 Google 网站上的所有用户签名。我之前曾将文档数据 API 与 2-legged OAuth 结合使用,效果很好。我目前陷入了电子邮件设置 API 的身份验证步骤。

代码示例:

// Setup OAuthServiceConfig
var oAuthConfig = UrlFetchApp.addOAuthService("signature");
oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken"); 
//I left scope empty to gain access to all APIs would this scope work scope=https://apps-apis.google.com/a/feeds/emailsettings/2.0/
oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oAuthConfig.setConsumerKey("domain.com");
oAuthConfig.setConsumerSecret("consumerSecret");

// Setup optional parameters to point request at OAuthConfigService.  The "signature"
// value matches the argument to "addOAuthService" above.
var options =
{
  "method" : method,
  "oAuthServiceName" : "signature",
  "oAuthUseToken" : "always"
};

var result = UrlFetchApp.fetch("https://apps-apis.google.com/a/feeds/emailsettings/2.0/"+domainName+"/"+userName+"/signature", options);
Logger.log(result);

我收到此错误:“意外错误(第 37 行)”,即 var result = UrlFetchApp.fetch("https://apps-apis.google.com/a/feeds/emailsettings/2.0/"+domainName+"/"+userName+"/signature", options);

对我做错了什么有什么想法吗?

范围在这里:http://support.google.com/a/bin/answer.py?hl=en&answer=162105

最佳答案

希望这对您有帮助。这是一个工作示例,它将获取用户的 HTML 签名或更新 HTML 签名

/*
----------------------------------------------------------------------------------
This function will update the HTML signature of a user.
Input will be jason data
To disable signature, pass an empty string as signature value
sample parameter
ob = {user='hps', signature='<b>Regards</b><br>Waqar'}

To disable signature
ob = {user='hps', signature=''}
----------------------------------------------------------------------------------
*/
function updateSignature(ob) {
  //ob = {};
  //ob.user = "hps";
  //ob.signature = "<b>Regards</b><br>Waqar";

  var base = 'https://apps-apis.google.com/a/feeds/emailsettings/2.0/';
  var xmlRaw = '<?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="'+htmlEncode(ob.signature)+'" />'+
      '</atom:entry>';
  var fetchArgs = googleOAuth_('emailSetting',base);
  fetchArgs.method = 'PUT';
  fetchArgs.payload = xmlRaw;
  fetchArgs.contentType = 'application/atom+xml';
  var domain = UserManager.getDomain();
  var url = base+domain+'/'+ob.user+'/signature';
  var urlFetch = UrlFetchApp.fetch(url, fetchArgs);
  var status = urlFetch.getResponseCode();
  return status;
}


//-----------------------------------------------------------------------------------------------------------
//This function will retreive Signature settings as json.
/*Sample returned object
{user=hps, signature=<b>Regards</b><br>Waqar}
*/
//-----------------------------------------------------------------------------------------------------------
function retrieveSignature(user) {
  var user = 'hps';
  var base = 'https://apps-apis.google.com/a/feeds/emailsettings/2.0/';
  var fetchArgs = googleOAuth_('emailSetting',base);
  fetchArgs.method = 'GET';
  var domain = UserManager.getDomain();
  var url = base+domain+'/'+user+'/signature?alt=json';
  var urlFetch = UrlFetchApp.fetch(url, fetchArgs);
  var jsonString = urlFetch.getContentText();
  var jsonArray = Utilities.jsonParse(jsonString).entry.apps$property;
  var ob = {};
  ob.user = user;
  for(var i in jsonArray){
    ob[jsonArray[i].name] = jsonArray[i].value;
  }
  return ob;
}
//Google oAuthConfig.. 
function googleOAuth_(name,scope) {
  var oAuthConfig = UrlFetchApp.addOAuthService(name);
  oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
  oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
  oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
  oAuthConfig.setConsumerKey("anonymous");
  oAuthConfig.setConsumerSecret("anonymous");
  return {oAuthServiceName:name, oAuthUseToken:"always"};
}


//This function will escape '<' and '>' characters from a HTML string
function htmlEncode(str){
  str = str.replace(/</g,'&lt;');
  return str.replace(/>/g,'&gt;')
}

关于电子邮件设置 API 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10805665/

相关文章:

email - 作为收件人,是否可以检测电子邮件是通过 Gmail 的 "Schedule Send"还是 "Send"发送的?

google-maps - 通过电子邮件发送谷歌静态 map

google-apps-script - 获取要插入到 Google 电子表格中的表单输入文本值

node.js - 错误 : Service accounts cannot invite attendees without Domain-Wide Delegation of Authority

google-analytics - 分析 Google API 错误 403 : “User does not have any Google Analytics Account”

python - 如何在线程池执行的函数中仅发送一次电子邮件?

Java 邮件消息传递异常

google-apps-script - 将两个谷歌表单数据合并到一个电子表格中

javascript - 从 Google 电子表格的下拉列表中选择多个值并按字母顺序打印

oauth - 如何更改 google API "service account"名称/电子邮件地址