【问题标题】:Get all events of user获取用户的所有事件
【发布时间】: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


【解决方案1】:

根据您的以下回复,

最有可能的凭据 = Credentials(get_access_token(request), scopes=SCOPES)

request.user_refresh_token 也是刷新令牌的值。

我还有一个 credentials.json 文件,其中包含用于登录的 token_uri、client_id 和 client_secret(如果正确)。

而且,您的问题是使用刷新令牌检索访问令牌。在这种情况下,为了获取访问令牌,下面的示例脚本怎么样?

示例脚本:

如果将client_idclient_secretrefresh_tokentoken_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_idclient_secretrefresh_tokentoken_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)

参考:

【讨论】:

  • 它给了我一个授权用户信息不是预期的格式,缺少字段 client_id、client_secret、refresh_token。
  • @Arundeep Chohan 感谢您的回复。我不得不为我糟糕的英语水平道歉。不幸的是,我无法理解It's giving me a Authorized user info was not in the expected format, missing fields client_id, client_secret, refresh_token.。可以问一下具体情况吗?
  • @Arundeep Chohan 顺便说一下,为了从刷新令牌中检索访问令牌,需要使用客户端 ID 和客户端密码。而且,当我测试Credentials()时,当tokentoken_uri没有被使用时,就会出现错误。所以我添加了它们。
  • 我确实使用了第一个,它手动设置它可能是它的结构问题,
  • 在配置变量中关闭这些值会更好吗?
猜你喜欢
  • 2015-09-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 2013-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-22
相关资源
最近更新 更多