【发布时间】:2022-02-15 09:42:50
【问题描述】:
所以目前我可以在使用 python 社交身份验证时使用 Google OAuth2 登录并将我的刷新令牌(如果需要,我还可以存储访问令牌)存储在数据库中。
models.py:
from django.db import models
from django.contrib.auth.models import AbstractUser
class Profile(AbstractUser):
refresh_token = models.CharField(max_length=255, default="")
我有一个 url /calendar 需要检索当前登录用户的 Google 日历事件。我也有一个常规登录,如果用户有一个刷新令牌,这意味着他已经将他的谷歌帐户链接到他的帐户。我将如何使用 get_events 来提供与该帐户关联的所有事件。
from googleapiclient.discovery import build
from google.oauth2.credentials import Credentials
def get_events(request):
print(request.user.refresh_token)
credentials = Credentials(get_access_token(request), scopes=SCOPES)
service = 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']
views.py
def login(request):
if request.method == 'POST':
form = AuthenticationForm(request.POST)
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user:
if user.is_active:
auth_login(request, user)
return redirect('home')
else:
messages.error(request,'User blocked')
return redirect('login')
else:
messages.error(request,'username or password not correct')
return redirect('login')
else:
form = AuthenticationForm()
return render(request, 'registration/login.html',{'form':form})
追溯:
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.
Request Method: GET
Request URL: http://localhost:8000/calendar
Django Version: 3.2.9
Exception Type: RefreshError
Exception Value:
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.
凭据.json
{
"web": {
"client_id": "-.apps.googleusercontent.com",
"project_id": "=",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_secret": "-",
"redirect_uris": ["http://127.0.0.1:8000/oauth/complete/google-oauth2/", "http://localhost:8000/oauth/complete/google-oauth2/"],
"javascript_origins": ["http://127.0.0.1:8000", "http://localhost:8000"]
}
}
【问题讨论】:
-
您的脚本当前的问题是什么?
-
通过刷新令牌刷新访问令牌的方法。它不会显示当前用户的日历事件。
-
我添加了回溯。最有可能的凭据 = Credentials(get_access_token(request), scopes=SCOPES)
-
感谢您的回复。您的问题是使用刷新令牌检索访问令牌。我的理解正确吗?如果我的理解是正确的,
How would I use get_events to just give all of the events associated with that account.是什么?而且,在您的脚本中,您已经能够检索刷新令牌。我的理解正确吗? -
是的,request.user_refresh_token 也是刷新令牌的值。
标签: django google-calendar-api python-social-auth