Google calendar api 接收GoogleAppClient.errors.HttpError:403,尽管是日历所有者和/或写入者
我遵循了,我的python脚本可以读取日历。但是在遵循了的指南之后,我的脚本出现了一个“权限不足”错误。我不确定它是否相关,但这是一个谷歌商业应用帐户,所以所有地址和ID都是“@mybusiness.com”,而不是“@gmail.com” 我在想我哪里出了错。任何帮助都将不胜感激 这是我的代码:Google calendar api 接收GoogleAppClient.errors.HttpError:403,尽管是日历所有者和/或写入者,google-calendar-api,Google Calendar Api,我遵循了,我的python脚本可以读取日历。但是在遵循了的指南之后,我的脚本出现了一个“权限不足”错误。我不确定它是否相关,但这是一个谷歌商业应用帐户,所以所有地址和ID都是“@mybusiness.com”,而不是“@gmail.com” 我在想我哪里出了错。任何帮助都将不胜感激 这是我的代码: #!/usr/bin/python from __future__ import print_function import httplib2 import os from apiclient im
#!/usr/bin/python
from __future__ import print_function
import httplib2
import os
from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools
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'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Google Calendar API Python Quickstart'
CALENDARID='redacted@redacted.com'
service=None
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 = oauth2client.file.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 read_calendar():
CAL=service.calendarList().get(calendarId=CALENDARID).execute()
print("Calendar Summary & Role:",CAL['summary'],CAL['accessRole'])
def insert_event():
event = {
'summary': 'Test Insert Event',
'location': 'Home',
'description': 'Automagic for the people',
'start': {
'dateTime': '2016-05-29T09:00:00-07:00',
'timeZone': 'America/Los_Angeles',
},
'end': {
'dateTime': '2016-05-29T10:00:00-07:00',
'timeZone': 'America/Los_Angeles',
},
'reminders': {
'useDefault': True,
},
}
event = service.events().insert(calendarId=CALENDARID, body=event).execute()
print('Event created: %s' % (event.get('htmlLink')))
def main():
global service
"""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')
eventsResult = service.events().list(
calendarId=CALENDARID, 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'))
end = event['end'].get('dateTime', event['end'].get('date'))
print(start,end,'Summary Redacted')
print("----------\n")
read_calendar()
insert_event()
if __name__ == '__main__':
main()
这是我的输出,减去10个事件的列表
$ ./gcal.py
Getting the upcoming 10 events
2016-06-29T09:00:00-04:00 2016-06-29T10:00:00-04:00 Summary Redacted
----------
<..etc... for 10 events, then -->
Calendar Summary & Role: redacted@redacted.com owner
Traceback (most recent call last):
File "./gcal.py", line 114, in <module>
main()
File "./gcal.py", line 110, in main
insert_event()
File "./gcal.py", line 79, in insert_event
event = service.events().insert(calendarId=CALENDARID, body=event).execute()
File "/usr/lib/python2.7/site-packages/oauth2client/util.py", line 137, in positional_wrapper
return wrapped(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/googleapiclient/http.py", line 760, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/calendar/v3/calendars/redacted%40redacted.com/events?alt=json returned "Insufficient Permission">
$。/gcal.py
获取即将到来的10项活动
2016-06-29T09:00:00-04:00 2016-06-29T10:00:00-04:00摘要已编辑
----------
日历摘要和角色:redacted@redacted.com所有者
回溯(最近一次呼叫最后一次):
文件“/gcal.py”,第114行,在
main()
文件“/gcal.py”,第110行,主
插入事件()
文件“/gcal.py”,插入事件第79行
event=service.events().insert(calendarId=calendarId,body=event).execute()
文件“/usr/lib/python2.7/site packages/oauth2client/util.py”,第137行,在位置包装中
已包装退货(*args,**kwargs)
文件“/usr/lib/python2.7/site packages/googleapiclient/http.py”,第760行,在execute中
raise HttpError(resp,content,uri=self.uri)
GoogleAppClient.errors.HttpError:
检查此SO问题和了解更多信息。检查此SO问题和了解更多信息。