此处仅需注意:尽管代码可以正常工作,但按照https://github.com/googleapis/google-auth-library-
python/blob/7a8641a7f0718c0dce413436f23691e8590face1/docs/index.rst的规定,oauth2client最近已被弃用,而支持google-
身份验证库-https: //github.com/googleapis/google-auth-library-
python/tree/edfe24602051969e32917e82bcedd2bace43e260
您可以在这里找到新库的文档-https: //google-auth.readthedocs.io/en/latest/user-
guide.html
要使用新库,可以将代码编写为
import datetimefrom datetime import timedeltaimport pytzfrom google.oauth2 import service_accountfrom googleapiclient.discovery import buildservice_account_email = "app-calendar@xxxxxxxxx.iam.gserviceaccount.com"SCOPES = ["https://www.googleapis.com/auth/calendar"]credentials = service_account.Credentials.from_service_account_file('google_calendar_credential.json')scoped_credentials = credentials.with_scopes(SCOPES)def build_service(): service = build("calendar", "v3", credentials=scoped_credentials) return servicedef create_event(): service = build_service() start_datetime = datetime.datetime.now(tz=pytz.utc) event = ( service.events() .insert( calendarId="primary", body={ "summary": "Foo 2", "description": "Bar", "start": {"dateTime": start_datetime.isoformat()}, "end": { "dateTime": (start_datetime + timedelta(minutes=15)).isoformat() }, }, ) .execute() ) print(event)create_event()
由于我的信誉不足,无法将其发布为评论,因此将其发布为单独的帖子
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)