在Google App Script中共享日历

时间:2014-11-23 21:32:43

标签: google-apps-script calendar google-oauth

我正在尝试从电子表格中构建日历,然后与域中的相应人员共享日历。我这样做是作为附加到电子表格的脚本。

到目前为止,我可以阅读正确的单元格并使用事件构建日历,但是我遇到问题的方法是找出如何与合适的人分享特定的日历。

我已经搜索过并找到相同的通用代码来手动请求网页允许我尝试使用的访问无效。我错过了其他人假设你做过的一步吗?我所做的只是在电子表格上写一个脚本,是否有一些设置可以让它工作?

如果通过脚本无法实现,那么处理此类问题的最佳方式是什么?我会更好地使用PHP和cron工作吗?

我目前共享日历的代码是:

function onEdit() {
 calendarSharing('nic@xyz.com', 'boris@xyz.com');
};

function calendarSharing(user1,user2){

    var scope = 'https://www.google.com/calendar/feeds/';
    var fetchArgs = googleOAuth_('calenders', scope);
    var rawXml= "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gAcl='http://schemas.google.com/acl/2007'>"+
      "<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/acl/2007#accessRule'/>"+
      "<gAcl:scope type='user' value='"+user2+"'></gAcl:scope>"+
      "<gAcl:role value='http://schemas.google.com/gCal/2005#owner'>  </gAcl:role></entry>"
    fetchArgs.method = 'POST';
    fetchArgs.payload = rawXml
    fetchArgs.contentType = 'application/atom+xml';

    try{
         var url='https://www.google.com/calendar/feeds/'+user1+'/acl/full'        
         var urlFetch=UrlFetchApp.fetch(url,fetchArgs)                         //Giving Permission To personal account as a owner
       }
    catch(err){
        var err1=err.toString()
        var ex='Exception: Request failed for  returned code 302'
        if(ex==err1.split('.')[0]){
              var tempCalenderUrl=[]
              tempCalenderUrl.id = err1.split('<A HREF="')[1];
              tempCalenderUrl.id = tempCalenderUrl.id.split('"')[0];
              var url=tempCalenderUrl.id
              try{
                  var urlFetch=UrlFetchApp.fetch(url,fetchArgs)   
                 }
              catch(err){}
         }         
     }
}

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"};
}

1 个答案:

答案 0 :(得分:4)

您可以使用Advanced Calendar Service来阅读和撰写日历共享信息。

此实用程序功能将在您具有writer权限的任何日历上插入或更新给定用户的共享权限。例如:

// Add user as reader
var rule = shareCalendar( 'gobbledygook@group.calendar.google.com',
                          'user@example.com');

代码

此功能也可作为this gist的一部分使用。

/**
 * Set up calendar sharing for a single user. Refer to 
 * https://developers.google.com/google-apps/calendar/v3/reference/acl/insert.
 *
 * @param {string} calId   Calendar ID
 * @param {string} user    Email address to share with
 * @param {string} role    Optional permissions, default = "reader":
 *                         "none, "freeBusyReader", "reader", "writer", "owner"
 *
 * @returns {aclResource}  See https://developers.google.com/google-apps/calendar/v3/reference/acl#resource
 */
function shareCalendar( calId, user, role ) {
  role = role || "reader";

  var acl = null;

  // Check whether there is already a rule for this user
  try {
    var acl = Calendar.Acl.get(calId, "user:"+user);
  }
  catch (e) {
    // no existing acl record for this user - as expected. Carry on.
  }

  if (!acl) {
    // No existing rule - insert one.
    acl = {
      "scope": {
        "type": "user",
        "value": user
      },
      "role": role
    };
    var newRule = Calendar.Acl.insert(acl, calId);
  }
  else {
    // There was a rule for this user - update it.
    acl.role = role;
    newRule = Calendar.Acl.update(acl, calId, acl.id)
  }

  return newRule;
}