电子邮件设置API认证 [英] Email Settings APIs Authentication

查看:152
本文介绍了电子邮件设置API认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用电子邮件设置API与Google Apps脚本来管理在谷歌网站的所有用户的签名。我以前使用过的文档数据API有两方模式OAuth和它的工作就好了。我目前停留在对电子邮件设置API认证的步骤。

I would like to use the Email Settings API with Apps Script to manage all users signatures on a Google Site. I have used Documents Data APIs before with 2-legged OAuth and it worked just fine. I am currently stuck on the authentication step for Email Settings API.

code例如:

// 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的结果= UrlFetchApp.fetch(\"https://apps-apis.google.com/a/feeds/emailsettings/2.0/\"+domainName+\"/\"+userName+\"/signature\",选项​​);

I get this error: "Unexpected Error (line 37)" which is var result = UrlFetchApp.fetch("https://apps-apis.google.com/a/feeds/emailsettings/2.0/"+domainName+"/"+userName+"/signature", options);

这是我在做什么错有什么想法?

Any thoughts on what I am doing wrong?

斯科普斯在这里:的http://支持。 google.com/a/bin/answer.py?hl=en&answer=162105

推荐答案

希望这会帮助你。这是一个工作的例子,这将让用户的HTML签名或更新HTML签名

Hope this will help you. This is an working example which will get the user's HTML signature or Update HTML signature

/*
----------------------------------------------------------------------------------
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认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆