2017-07-28 76 views
0

我有一个将运行cron操作的python脚本。 它需要查找当天的事件列表,然后根据事件执行一些其他操作。 到目前为止,我可以向Calendar API发出请求,并获取当天事件列表。 当我循环显示事件项目列表时,该项目的“摘要”键缺失。 我需要这个字段,以便我可以确定事件的目的。Python Google Calendar API v3在列出事件项时不会返回“摘要”字段

在每个事件项目对应的数据是回来了类似这样的没有“摘要”键

{ 
    "status": "confirmed", 
    "kind": "calendar#event", 
    "end": { 
    "date": "2017-07-29" 
    }, 
    "iCalUID": "[email protected]", 
    "updated": "2017-06-20T00:00:00.000Z", 
    "start": { 
    "date": "2017-07-24" 
    }, 
    "etag": "\"0000000000000000\"", 
    "id": "0000000000000" 
} 

在这里找到https://developers.google.com/google-apps/calendar/v3/reference/events#resource它显示了“摘要”键应该与该事件被返回的谷歌文档。

由于此脚本将自动运行,因此我设置了一个Google Service帐户来授予对API的请求,以便用户不必手动对其进行授权。下面是脚本的样本,我使用

# -*- coding: utf-8 -*- 
from oauth2client.service_account import ServiceAccountCredentials 
from httplib2 import Http 
from apiclient.discovery import build 
import datetime 

try: 
    scopes = ['https://www.googleapis.com/auth/calendar'] 

    credentials = ServiceAccountCredentials.from_json_keyfile_name(
     '/path/to/credentials/filename.json', scopes=scopes) 

    http_auth = credentials.authorize(Http()) 

    startTime = str(datetime.datetime.now().date()) + 'T00:00:00-07:00' 
    endTime = str(datetime.datetime.now().date()) + 'T23:59:00-07:00' 
    calendar = build('calendar', 'v3', http=http_auth) 
    calId = 'calendar_id_string' 

    response = calendar.events().list(calendarId=calId, 
    timeMin=startTime, timeMax=endTime).execute() 

    items = response.get('items',[]) 
    for item in items: 
     summary = item.get('summary','') # summary is blank!! 

except Exception, ex: 
    print ex 

感谢您的帮助

回答

0

为什么事件“摘要”并没有返回到客户端的原因是因为这是一个日历权限设置为电子邮件帐户。

电子邮件帐户来自我创建服务帐户时创建的凭据JSON文件。

当日历与电子邮件帐户共享时,管理员将权限设置为“仅查看忙/闲(隐藏详细信息)”,而不是“查看所有活动详情”。

使用“仅查看空闲/忙碌(隐藏详细信息)”权限,用户只能查看日历在给定时间是空闲还是忙碌,但不返回事件详细信息。

通过改变这个权限为“查看所有详细活动”,那么所有的事件的详细信息将被返回给客户端,包括“摘要”关键是我所需要的。

在这里看到更多的细节:

https://developers.google.com/google-apps/calendar/concepts/sharing https://developers.google.com/api-client-library/python/auth/service-accounts

只有日历拥有者可以更改日历上的权限。如果你是那么业主,

  • 登录到谷歌日历

  • 点击设置齿轮按钮

  • 单击设置>日历> [您的日历名称]>共享此日历

  • 查找电子邮件帐户并更改权限设置

  • Clic k保存

否则,您需要联系店主更改权限。

0

如果您拥有G套件帐户并拥有自己的域名,则域的管理员必须按照这些说明授权客户端。

https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority

注 - 为授权客户端,你必须已经创建了“启用摹套房域范围内的代表团”检查服务帐户。然后将client_id与范围一起提供给管理员,以便管理员可以授权。

https://developers.google.com/identity/protocols/OAuth2ServiceAccount#authorizingrequests

服务帐户后,与已启用“G套房域范围内的代表团”,并在客户端和范围是由域管理员授权,那么,你应该能够看到所有的事件细节在完成api调用之后。下面是一个例子。希望这可以帮助其他人

# -*- coding: utf-8 -*- 
from oauth2client.service_account import ServiceAccountCredentials 
from httplib2 import Http 
from apiclient.discovery import build 
import datetime 

try: 
    # updated the scopes with "calendar.readonly" 
    scopes = ['https://www.googleapis.com/auth/calendar.readonly'] 

    credentials = ServiceAccountCredentials.from_json_keyfile_name(
     '/path/to/credentials/filename.json', scopes=scopes) 

    # create a credential object with an authorized user with read access to the calendar 
    delegated_credentials = credentials.create_delegated('[email protected]_domain.com') 
    http_auth = delegated_credentials.authorize(Http()) 

    startTime = str(datetime.datetime.now().date()) + 'T00:00:00-07:00' 
    endTime = str(datetime.datetime.now().date()) + 'T23:59:00-07:00' 
    calendar = build('calendar', 'v3', http=http_auth) 
    calId = 'calendar_id_string' 

    response = calendar.events().list(calendarId=calId, timeMin=startTime, timeMax=endTime).execute() 

    items = response.get('items',[]) 
    for item in items: 
     summary = item.get('summary','') # Event summary exists!! Yea!! 

except Exception, ex: 
    print ex 
相关问题