Bea*_*sIV 0 calendar google-apps-script 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"};
}
Run Code Online (Sandbox Code Playgroud)
您可以使用高级日历服务读取和写入日历共享信息。
此实用程序函数将在您拥有权限的任何日历上插入或更新给定用户的共享权限writer。例如:
// Add user as reader
var rule = shareCalendar( 'gobbledygook@group.calendar.google.com',
'user@example.com');
Run Code Online (Sandbox Code Playgroud)
此功能也可以作为本要点的一部分提供。
/**
* 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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3874 次 |
| 最近记录: |