python - 任何无需 token.pickle 即可创建新 Google 日历事件的方法

标签 python google-api google-oauth google-calendar-api google-api-python-client

使用 token.pickle 非常令人沮丧,因为 token 每隔几周就会过期,然后我需要在代码中手动将其从源文件中删除,以便它可以自行重新生成,然后我需要重新验证它从我的帐户。

有没有办法我可以在没有它的情况下创建新服务?我知道 Google 表格文件是可能的。看起来像这样:

def get_g_sheets_service():
 SERVICE_ACCOUNT_FILE = 'key.json'
 SCOPES = ['https://www.googleapis.com/auth/spreadsheets']

 creds = None
 creds = service_account.Credentials.from_service_account_file(
    SERVICE_ACCOUNT_FILE, scopes=SCOPES)

 SCOPES = ['https://www.googleapis.com/auth/spreadsheets']

 SAMPLE_SPREADSHEET_ID = 'ID_GOES_HERE'
 service = build('sheets', 'v4', credentials=creds)
 return service

但是,获取日历 API 服务的方式如下所示:

import datetime
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/calendar']

CREDENTIALS_FILE = 'path_to_file/credentials.json'

def get_calendar_service():
   creds = None
   # The file token.pickle 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.pickle'):
       with open('token.pickle', 'rb') as token:
           creds = pickle.load(token)
   # 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_FILE, SCOPES)
           creds = flow.run_local_server(port=0)

       # Save the credentials for the next run
       with open('token.pickle', 'wb') as token:
           pickle.dump(creds, token)

   service = build('calendar', 'v3', credentials=creds)
   return service

注意到 token.pickle 文件了吗?

我怎么能不应对呢?

最佳答案

为什么 token.pickle 会过期?

token.pickle 应包含一个访问 token 和一个刷新 token ,该 token 是在用户同意您的应用程序访问其数据时创建的。访问 token 用于访问用户数据并在一小时后过期,刷新 token 用于在过期时请求新的访问 token 。这是通过您正在使用的客户端库自动完成的。大多数情况下,刷新 token 不应过期 see: experation .

您需要确保始终存储最新的刷新 token 。

如果您的应用程序仍处于测试阶段,刷新 token 将在 7 天后过期。 see: experation

A Google Cloud Platform project with an OAuth consent screen configured for an external user type and a publishing status of "Testing" is issued a refresh token expiring in 7 days.

解决方案是在 oauth2 同意屏幕下转到 Google Cloud 控制台,并将您的应用程序设置为生产。

服务帐户

如果此 Google 日历是 Google Workspace 的一部分。那么您的工作区管理员可以授予服务帐户域范围的委派,并允许您使用该服务帐户模拟域上的用户。这种授权形式更加简单,并且不会出现与通过工作区配置授权相同的过期 token 问题。

服务帐户只能通过具有工作区域帐户的 Google 日历工作。

关于python - 任何无需 token.pickle 即可创建新 Google 日历事件的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72108285/

相关文章:

python - 如何在Google Cloud深度学习虚拟机上安装tensorflow-transform?

javascript - 谷歌地图 HTML/JavaScript 不显示

javascript - Firebase 的“每个电子邮件地址一个帐户”允许 Google OAuth 在未经同意的情况下替换电子邮件/密码帐户

node.js - Google Translate API 总是返回 "Required parameter: q"作为错误

javascript - Google 从本地主机登录中的 idpiframe_initialization_failed

c# - 如何使用 ASP.Net Core Identity 从登录用户检索 Google 个人资料图片?

python - 通过python加载到MySQL的数据消失了

python - str.cat 忽略 `na_rep` 参数?

python pig 拉丁语转换器

google-api - 如何通过 API 向 Google Drive 添加/创建/插入文件?