2014-04-11 56 views
0

所以我想創建一個新的日曆,但我想能夠指定什麼谷歌帳戶來創建它,假設我有所述帳戶的憑據,我這樣做。下面的代碼在當前登錄的用戶身上創建它,或者要求用戶交互以允許訪問。有沒有辦法指定用戶並在後臺運行該命令。我基本上只是想在程序運行時將一個日曆添加到我的帳戶,但我無法保證我的帳戶將在當時登錄。如何以不同的用戶身份登錄Google API v3?

我相信這是可能通過ClientLogin用戶的谷歌API的版本2,但我想使用版本3

import gflags 
import httplib2 

from apiclient.discovery import build 
from oauth2client.file import Storage 
from oauth2client.client import OAuth2WebServerFlow 
from oauth2client.tools import run 

FLAGS = gflags.FLAGS 
FLOW = OAuth2WebServerFlow(
    client_id='MY_CLIENT_KEY', 
    client_secret='MY_CLIENT_SECRET', 
    scope='https://www.googleapis.com/auth/calendar', 
    user_agent='MyApp/v1') 
storage = Storage('calendar.dat') 
credentials = storage.get() 
if credentials is None or credentials.invalid == True: 
    credentials = run(FLOW, storage) 

http = httplib2.Http() 
http = credentials.authorize(http) 
service = build(serviceName='calendar', version='v3', http=http) 

calendar = { 
    'summary': 'Test3', 
    'timeZone': 'America/New_York' 
} 

created_calendar = service.calendars().insert(body=calendar).execute() 

回答

1

隨着V3,你需要使用一個服務帳戶爲了充當用戶。該流程在Google Drive documentation中描述,您只需使用Calendar API v3範圍和引用,而不是Google Drive API。

相關問題