使用Google日历API.有没有办法打印出API响应代码



我目前正在处理一个使用Google日历API的项目。我希望我正在创建的程序打印出一个";成功;当它成功地连接到API或;失败";但事实并非如此。此外,想知道是否有任何方法可以专门打印出API响应代码?

您是否已经阅读了Python的Google Calendar API教程?

从下面的代码片段中,您应该在生成凭据和实例化客户端时检查错误/异常。我已将它们标记为TODO#1、#2和#3。

有关其他上下文,请参阅示例。祝你好运

def main():
"""Shows basic usage of the Google Calendar API.
Prints the start and name of the next 10 events on the user's calendar.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())  # TODO #1: Add exception handling here (and really the entire block) to capture authentication/authorization issues
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('calendar', 'v3', credentials=creds)  # TODO #2: Add exception handling here to capture client side issues
# Call the Calendar API
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
print('Getting the upcoming 10 events')
# TODO #3: Add exception handling here in the actual API call
events_result = service.events().list(calendarId='primary', timeMin=now,
maxResults=10, singleEvents=True,
orderBy='startTime').execute()
events = events_result.get('items', [])

最新更新