2017-07-28 56 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 
相關問題