访问事件的范围不正确
Not correct scope for accessing events
HttpError at /calendar
<HttpError 403 when requesting https://www.googleapis.com/calendar/v3/calendars/primary/events?singleEvents=true&orderBy=startTime&alt=json returned "Request had insufficient authentication scopes.". Details: "[{'message': 'Insufficient Permission', 'domain': 'global', 'reason': 'insufficientPermissions'}]">
Request Method: GET
Request URL: http://localhost:8000/calendar
Django Version: 3.2.9
Exception Type: HttpError
Exception Value:
过一会儿再做这个
RefreshError at /calendar
The credentials do not contain the necessary fields need to refresh the access token. You must specify refresh_token, token_uri, client_id, and client_secret.
我似乎在访问日历时没有正确的范围,目前看来 access_token 确实出现了。
from google.oauth2.credentials import Credentials
def get_user_events(request):
credentials = Credentials(get_access_token(request), scopes=SCOPES)
service = googleapiclient.discovery.build('calendar', 'v3', credentials=credentials)
google_calendar_events = service.events().list(calendarId='primary', singleEvents=True,
orderBy='startTime').execute()
google_calendar_events = google_calendar_events.get('items', [])
return google_calendar_events
def get_access_token(request):
social = request.user.social_auth.get(provider='google-oauth2')
return social.extra_data['access_token']
所以我使用的范围如下
SCOPES = ['https://www.googleapis.com/auth/calendar']
"Request had insufficient authentication scopes.".
如果我们查看 events.get 的文档,我们会发现此方法对私有用户数据进行操作,这意味着我们需要获得授权才能访问它。最重要的是,我们需要获得以下范围之一的授权
我无法从您的代码中看出您请求的范围。要做的第一件事是确保您在授权用户时请求上述范围之一。如果您更改范围,请记住您必须再次请求用户同意才能生效。
You must specify refresh_token, token_uri, client_id, and client_secret.
听起来您没有正确存储刷新令牌。 get_access_token 如果没有存储刷新令牌,将无法工作。
在官方示例中,请注意他们如何将用户凭据存储在 token.json 中,然后库将在需要时自动请求新的访问令牌。
"""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.json 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.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# 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())
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.json', 'w') as token:
token.write(creds.to_json())
HttpError at /calendar <HttpError 403 when requesting https://www.googleapis.com/calendar/v3/calendars/primary/events?singleEvents=true&orderBy=startTime&alt=json returned "Request had insufficient authentication scopes.". Details: "[{'message': 'Insufficient Permission', 'domain': 'global', 'reason': 'insufficientPermissions'}]"> Request Method: GET Request URL: http://localhost:8000/calendar Django Version: 3.2.9 Exception Type: HttpError Exception Value:
过一会儿再做这个
RefreshError at /calendar The credentials do not contain the necessary fields need to refresh the access token. You must specify refresh_token, token_uri, client_id, and client_secret.
我似乎在访问日历时没有正确的范围,目前看来 access_token 确实出现了。
from google.oauth2.credentials import Credentials
def get_user_events(request):
credentials = Credentials(get_access_token(request), scopes=SCOPES)
service = googleapiclient.discovery.build('calendar', 'v3', credentials=credentials)
google_calendar_events = service.events().list(calendarId='primary', singleEvents=True,
orderBy='startTime').execute()
google_calendar_events = google_calendar_events.get('items', [])
return google_calendar_events
def get_access_token(request):
social = request.user.social_auth.get(provider='google-oauth2')
return social.extra_data['access_token']
所以我使用的范围如下
SCOPES = ['https://www.googleapis.com/auth/calendar']
"Request had insufficient authentication scopes.".
如果我们查看 events.get 的文档,我们会发现此方法对私有用户数据进行操作,这意味着我们需要获得授权才能访问它。最重要的是,我们需要获得以下范围之一的授权
我无法从您的代码中看出您请求的范围。要做的第一件事是确保您在授权用户时请求上述范围之一。如果您更改范围,请记住您必须再次请求用户同意才能生效。
You must specify refresh_token, token_uri, client_id, and client_secret.
听起来您没有正确存储刷新令牌。 get_access_token 如果没有存储刷新令牌,将无法工作。
在官方示例中,请注意他们如何将用户凭据存储在 token.json 中,然后库将在需要时自动请求新的访问令牌。
"""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.json 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.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# 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())
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.json', 'w') as token:
token.write(creds.to_json())