获取用户的所有事件
Get all events of user
所以目前我可以使用 Google OAuth2 在数据库中登录并存储我的刷新令牌(如果需要我也可以存储访问令牌),同时使用 python 社交身份验证。
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 日历事件。我还有一个常规登录,如果用户有刷新令牌,这意味着他已将他的 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.
credentials.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"]
}
}
根据您的以下回复,
It's most likely credentials = Credentials(get_access_token(request), scopes=SCOPES)
request.user_refresh_token is the value of the refresh token as well.
I also have a credentials.json file that contains a token_uri, client_id, and client_secret if that is correct which was used for login.
并且,您的问题是使用刷新令牌检索访问令牌。在这种情况下,为了检索访问令牌,下面的示例脚本怎么样?
示例脚本:
如果client_id
、client_secret
、refresh_token
和token_uri
都放在一个变量中,可以使用下面的脚本。
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
credentials = Credentials(
token=None,
client_id="###", # Please set the cliend ID.
client_secret="###", # Please set client secret.
refresh_token='###', # Please set refresh token.
token_uri="###" # Please set token URI.
)
credentials.refresh(Request())
access_token = credentials.token
print(access_token)
# If you want to use the following script, you can use it.
# service = build('calendar', 'v3', credentials=credentials)
或者,如果client_id
、client_secret
、refresh_token
和token_uri
放在一个文件中,你可以使用下面的脚本。
tokenFile = '###' # Please set the filename with the path.
credentials = Credentials.from_authorized_user_file(tokenFile) # or, Credentials.from_authorized_user_file(tokenFile, scopes)
credentials.refresh(Request())
access_token = credentials.token
print(access_token)
# If you want to use the following script, you can use it.
# service = build('calendar', 'v3', credentials=credentials)
参考:
所以目前我可以使用 Google OAuth2 在数据库中登录并存储我的刷新令牌(如果需要我也可以存储访问令牌),同时使用 python 社交身份验证。
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 日历事件。我还有一个常规登录,如果用户有刷新令牌,这意味着他已将他的 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.
credentials.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"]
}
}
根据您的以下回复,
It's most likely credentials = Credentials(get_access_token(request), scopes=SCOPES)
request.user_refresh_token is the value of the refresh token as well.
I also have a credentials.json file that contains a token_uri, client_id, and client_secret if that is correct which was used for login.
并且,您的问题是使用刷新令牌检索访问令牌。在这种情况下,为了检索访问令牌,下面的示例脚本怎么样?
示例脚本:
如果client_id
、client_secret
、refresh_token
和token_uri
都放在一个变量中,可以使用下面的脚本。
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
credentials = Credentials(
token=None,
client_id="###", # Please set the cliend ID.
client_secret="###", # Please set client secret.
refresh_token='###', # Please set refresh token.
token_uri="###" # Please set token URI.
)
credentials.refresh(Request())
access_token = credentials.token
print(access_token)
# If you want to use the following script, you can use it.
# service = build('calendar', 'v3', credentials=credentials)
或者,如果client_id
、client_secret
、refresh_token
和token_uri
放在一个文件中,你可以使用下面的脚本。
tokenFile = '###' # Please set the filename with the path.
credentials = Credentials.from_authorized_user_file(tokenFile) # or, Credentials.from_authorized_user_file(tokenFile, scopes)
credentials.refresh(Request())
access_token = credentials.token
print(access_token)
# If you want to use the following script, you can use it.
# service = build('calendar', 'v3', credentials=credentials)