更新时间:2023-12-04 15:55:34
I think you're problem is that your not authorising your credentials before making the call to the Calendar API but there is a couple of differences that you have that I use in my own implementation
from oauth2client.service_account import ServiceAccountCredentials
和from apiclient import discovery
from_json_keyfile_name
方法http
参数传递from oauth2client.service_account import ServiceAccountCredentials
and from apiclient import discovery
from_json_keyfile_name
methodhttp
argument when building the service尝试对您的代码进行此修改:
Try this modification to your code:
from apiclient import discovery
from oauth2client.service_account import ServiceAccountCredentials
import httplib2
SCOPES = ['https://www.googleapis.com/auth/calendar']
SERVICE_ACCOUNT_FILE = 'GOOGLE_SECRET.json'
credentials = ServiceAccountCredentials.from_json_keyfile_name(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
# Use the create_delegated() method and authorize the delegated credentials
delegated_credentials = credentials.create_delegated('address@example.com')
delegated_http = delegated_credentials.authorize(httplib2.Http())
google_calendar = discovery.build('calendar', 'v3', http=delegated_http)
events = google_calendar.events().list(
calendarId='address@example.com',
maxResults=10
).execute()
我还建议您在API调用中设置calendarId='primary'
,以便始终返回当前为其委派了凭据的用户的主日历.
I would also suggest setting calendarId='primary'
in your API call so that you always return the primary calendar of the user who you currently have delegated credentials for.
有关使用ServiceAccountCredentials
进行身份验证的更多信息,请参见以下链接: http://oauth2client.readthedocs.io/en/latest/source/oauth2client.service_account.html
For more information on authenticating using ServiceAccountCredentials
see the following link: http://oauth2client.readthedocs.io/en/latest/source/oauth2client.service_account.html