我正在创建一个 MacOS 应用程序,该应用程序在给定的 Google Team Drive/文件夹中创建自定义文件夹层次结构。每次我运行该应用程序时,它都会打开一个浏览器并要求用户登录并允许该应用程序访问。接受后,应用程序会创建文件夹并按预期工作。但是,它每次都会这样做,我很难找出如何保存“用户的同意”,以便他们只需要允许一次同意,即他们第一次运行应用程序时。我尝试了许多不同的选择,但我一无所获,所以我非常感谢任何帮助。
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from google_auth_oauthlib.flow import InstalledAppFlow
SCOPES = ['https://www.googleapis.com/auth/drive']
API_SERVICE_NAME = 'drive'
API_VERSION = 'v3'
def get_authenticated_service():
client_secrets = args["secrets"] #client_secrets.json file passed as "secrets" argument
flow = InstalledAppFlow.from_client_secrets_file(client_secrets, SCOPES)
credentials = flow.run_local_server(host='localhost',
port=8080,
authorization_prompt_message='Please visit this URL: {url}',
success_message='The auth flow is complete; you may close this window.',
open_browser=True)
return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)
if __name__ == '__main__':
service = get_authenticated_service()
name = format_client_name()
create_folders(service, name)
我在这里尝试使用 Google API 客户端库(https://developers.google.com/api-client-library/python/guide/aaa_oauth),它显示存储,但是它没有任何与谷歌相关的说明。 oauth,因为您不能将 oauth2client 存储与 google.oauth 一起使用。我觉得这很简单,但我碰壁了。