python - 为连接到 Google Calendar API 的个人使用应用程序生成持久 token

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

我正在尝试设置一个供个人使用的应用程序,用于向 Google 日历事件参加者发送提醒电子邮件。根据谷歌的app verification exceptions ,我不需要验证个人使用的应用程序。但是,我的访问 token 在 7 天后不断过期,并且我刷新 token 的尝试都是徒劳的。我意识到发布状态为“测试”的应用程序会在 7 天后过期,但唯一的其他选择是提交验证。也许 Google Calendar API 被定义为敏感范围这一事实意味着我必须提交验证,即使我是唯一使用它的人?

运行以下代码会返回错误 google.auth.exceptions.RefreshError: ('invalid_grant: token 已过期或已撤销。' 我的 token.json 文件包含tokenrefresh_token 的值。

提醒.py:

basedir = os.path.abspath(os.path.dirname(__file__))
load_dotenv(os.path.join(basedir, '.env'))

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

def main():
    """Shows basic usage of the Google Calendar API.
    Prints the start and name of the next 10 events on the user's calendar.
    """
    flow = Flow.from_client_secrets_file(
                os.path.join(basedir, 'credentials.json'), SCOPES)

    authorization_url, state = flow.authorization_url(
    # Enable offline access so that you can refresh an access token without
    # re-prompting the user for permission. Recommended for web server apps.
    access_type='offline',
    # Enable incremental authorization. Recommended as a best practice.
    include_granted_scopes='true')

    creds = None
    # The file token.json 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.json'):
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)
    # 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(
                os.path.join(basedir, 'credentials.json'), SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.json', 'w') as token:
            token.write(creds.to_json())

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

最佳答案

您的代币是Expireing 7 天后,因为您的应用程序仍在测试中。

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.

解决方案是将您的项目设置为生产而不是测试。这应该可以防止您的 token 过期。您不需要发送它进行验证,只需将其切换到生产环境即可。文档中的任何内容以及我自己和其他人的测试都表明您“必须”进行验证以阻止刷新 token 过期。

enter image description here

内部

内部项目仅适用于 Google Workspace 帐户。因此,除非您有工作区帐户,否则您无法将项目设置为内部。如果您确实有工作区帐户并且该用户位于工作区域中,那么您应该考虑使用服务帐户。但是,如果您选择坚持使用 oauth2,则刷新 token 应该可以工作超过 7 天(如果是内部的),但是目前 google 正在研究一个错误,该错误可能会导致刷新 token 在内部工作区项目上过期。最后我听说他们正在调查此事。

关于python - 为连接到 Google Calendar API 的个人使用应用程序生成持久 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68776920/

相关文章:

python - Wandisco:通过 REST 创建新的复制规则

python for循环增量值与输入不一样

python - SQLAlchemy:__tablename__ 作为变量

php - 需要帮助从 Laravel 5 应用程序引用 PECL OAuth

node.js - spotify-web-api- Node : authorizationCodeGrant() give a 400 - Bad Request

security - 单页 Web 应用程序、CORS 和安全问题

google-play - 已发布游戏上的 "The Application is incorrectly configured"

python - PyCharm 3.0 找不到 PyQt5

node.js - 使用 Angular 和 NodeJS 进行 Google 2 因素身份验证

rest - 为什么 google-oauth API 需要重定向 URL?