Google 日历 API 日历 ID 的 HttpError 404
Google Calendar API HttpError 404 for calendar ID
我使用的是 Google Calendar API Python 快速入门代码的修改版本,它对我来说工作了一段时间。我最近刚买了一台新电脑,并且一直在向它传输文件。现在当我 运行 脚本时,我得到一个 HTTPError 404。代码如下:
from __future__ import print_function
import httplib2
import os
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
import datetime
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/calendar-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/calendar.readonly'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Google Calendar API Python Quickstart'
def get_credentials():
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'calendar-python-quickstart.json')
store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def main():
"""Shows basic usage of the Google Calendar API.
Creates a Google Calendar API service object and outputs a list of the next
10 events on the user's calendar.
"""
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('calendar', 'v3', http=http)
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC
time
print('Getting the upcoming 10 events')
"""Below, I switched out 'primary' with the calendarId of the calendar I
actually wanted"""
eventsResult = service.events().list(
calendarId='redacted@group.calendar.google.com', timeMin=now,
maxResults=10, singleEvents=True,
orderBy='startTime').execute()
events = eventsResult.get('items', [])
if not events:
print('No upcoming events found.')
for event in events:
start = event['start'].get('dateTime', event['start'].get('date'))
print(start, event['summary'])
if __name__ == '__main__':
main()
我注意到当我将 'calendarId' 替换为 'primary' 而不是我想要的日历 ID 时,代码工作正常。当这个完全相同的代码在我以前的机器上运行良好时,我不明白为什么我会收到错误消息。以下是完整的错误消息(已编辑):
Traceback (most recent call last):
File "C:\redacted.py", line 84, in <module>
main()
File "C:\redacted.py", line 73, in main
orderBy='startTime').execute()
File "C:\redacted\Python\Python36-32\lib\site-
packages\oauth2client\_helpers.py", line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\redacted\Python\Python36-32\lib\site-
packages\googleapiclient\http.py", line 844, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 404 when requesting
https://www.googleapis.com/calendar/v3/calendars/redacted=json returned
"Not Found">
任何人都可以帮助解释我所缺少的东西吗?
它应该是使用您的应用程序的用户的电子邮件地址。此外,如果您访问的不是他们自己的日历 ID,则应将其共享给访问它的用户。
calendarId
- Calendar identifier. To retrieve calendar IDs call the
calendarList.list
method. If you want to access the primary calendar of the currently logged in user, use the "primary
" keyword.
见下图:
希望这对您有所帮助。
我使用的是 Google Calendar API Python 快速入门代码的修改版本,它对我来说工作了一段时间。我最近刚买了一台新电脑,并且一直在向它传输文件。现在当我 运行 脚本时,我得到一个 HTTPError 404。代码如下:
from __future__ import print_function
import httplib2
import os
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
import datetime
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/calendar-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/calendar.readonly'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Google Calendar API Python Quickstart'
def get_credentials():
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'calendar-python-quickstart.json')
store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def main():
"""Shows basic usage of the Google Calendar API.
Creates a Google Calendar API service object and outputs a list of the next
10 events on the user's calendar.
"""
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('calendar', 'v3', http=http)
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC
time
print('Getting the upcoming 10 events')
"""Below, I switched out 'primary' with the calendarId of the calendar I
actually wanted"""
eventsResult = service.events().list(
calendarId='redacted@group.calendar.google.com', timeMin=now,
maxResults=10, singleEvents=True,
orderBy='startTime').execute()
events = eventsResult.get('items', [])
if not events:
print('No upcoming events found.')
for event in events:
start = event['start'].get('dateTime', event['start'].get('date'))
print(start, event['summary'])
if __name__ == '__main__':
main()
我注意到当我将 'calendarId' 替换为 'primary' 而不是我想要的日历 ID 时,代码工作正常。当这个完全相同的代码在我以前的机器上运行良好时,我不明白为什么我会收到错误消息。以下是完整的错误消息(已编辑):
Traceback (most recent call last):
File "C:\redacted.py", line 84, in <module>
main()
File "C:\redacted.py", line 73, in main
orderBy='startTime').execute()
File "C:\redacted\Python\Python36-32\lib\site-
packages\oauth2client\_helpers.py", line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\redacted\Python\Python36-32\lib\site-
packages\googleapiclient\http.py", line 844, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 404 when requesting
https://www.googleapis.com/calendar/v3/calendars/redacted=json returned
"Not Found">
任何人都可以帮助解释我所缺少的东西吗?
它应该是使用您的应用程序的用户的电子邮件地址。此外,如果您访问的不是他们自己的日历 ID,则应将其共享给访问它的用户。
calendarId
- Calendar identifier. To retrieve calendar IDs call the
calendarList.list
method. If you want to access the primary calendar of the currently logged in user, use the "primary
" keyword.
见下图:
希望这对您有所帮助。