任何方法可以创建新的Google日历事件,而无需token.pickle

发布于 2025-01-26 04:49:14 字数 1950 浏览 3 评论 0原文

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

有什么方法可以在没有它的情况下创建一项新服务?我知道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文件吗?

我怎么不处理它?

Using token.pickle is quite frustrating, as every couple of weeks the token expires, and then I need to manually delete it from my source files in code, so it can regenerate itself, and then I need to re-authenticate it from my account.

Is there a way I can just create a new service without it? I know it's possible for Google sheets files. This is what that looks like:

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

but, the way to get a service for the calendar API looks like this:

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

Notice the token.pickle file?

How can I not deal with it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满身野味 2025-02-02 04:49:14

为什么token.pickle expring?

token.pickle应包含访问令牌和一个刷新令牌,该令牌是在用户同意您的应用程序访问其数据的情况下创建的。访问令牌用于访问用户数据并在一个小时后到期,刷新令牌用于在过期后要求新的访问令牌。这是通过您使用的客户端库自动完成的。刷新代币大部分不应到期请参见:seac each

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

如果您的应用程序仍处于测试阶段,则刷新令牌将在7天后到期。 请参阅:实验

与外部用户类型配置的OAuth同意屏幕和“测试”的发布状态的Google Cloud Platform项目在7天内发行了刷新令牌。

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

服务帐户

如果此Google日历是Google Workspace的一部分。然后,您的工作空间管理员可以授予服务帐户域范围内的代表团,并允许您使用服务帐户模仿域上的用户。此授权表格更加轻松,并且与通过工作空间配置的授权相同的到期令牌问题。

服务帐户仅通过使用工作空间域帐户的Google日历来工作。

why is the token.pickle expring?

The token.pickle should contain an access token and a refresh token which was created with the user consented to your application accessing their data. Access tokens are used to access user data and expire after an hour, refresh tokens are use to request a new access token when it has expired. This is done automatically via the client library you are using. Refresh tokens for the most part should not be expiring see: experation.

You need to be sure you are always storing the most recent refresh token.

If your application is still in the testing phase refresh tokens will expire after seven days. 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.

The solution is to go to google cloud console under the oauth2 consent screen and set your application to production.

service accounts

If this google calendar is part of google workspace. then your workspace admin could grant a service account domain wide delegation and allow you to impersonate a user on the domain with the service account. This form for authorization is much easer and will not have the same expiration token issues as the authorization is configured via workspace.

Service accounts only work though google calendar with workspace domain accounts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文