用Python存储Google身份验证凭据

2024-05-20 08:46:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我从googleapi文档中获取了相同的代码。我抓取的代码提示用户授权我的应用程序可以使用他们的帐户,然后像一个视频。这是密码

import os

import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors

scopes = ["https://www.googleapis.com/auth/youtube.force-ssl"]

def main():
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "YOUR_CLIENT_SECRET_FILE.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

    request = youtube.videos().rate(
        id="eyRGEv3XAxk",
        rating="like"
    )
    request.execute()

if __name__ == "__main__":
    main()

此代码与有效的“客户机机密文件”一起工作。但是有没有什么方法可以让我保存凭据,这样我就不必总是提示用户授权我的应用程序了?假设我想在我的代码的另一部分喜欢另一个视频,但我不想再次授权


Tags: 代码用户nameimportclientauthapi应用程序