如何通过API对个人Google Keep账号执行增删改查操作?
操作个人Google Keep数据的可行方案
可以通过API操作个人Google Keep账号数据,但服务账号无法实现,必须使用OAuth 2.0授权流程,以下是具体方法:
核心原因说明
Google Keep不支持服务账号通过协作者共享或域委派访问个人账号数据,服务账号是独立的身份实体,无法关联到个人Keep的笔记内容,这也是你添加协作者后自动消失、调用返回空结果的原因。
正确实现步骤
1. 配置OAuth 2.0客户端凭证
- 回到Google Cloud控制台,进入已启用Keep API的项目
- 在「API和服务」→「凭据」页面,点击「创建凭据」→「OAuth客户端ID」
- 应用类型选择「桌面应用」,填写名称后创建,下载生成的
client_secret.json文件
2. 确认权限范围
Google Keep API提供两个有效权限范围:
https://www.googleapis.com/auth/keep:全读写权限(支持编辑、添加、删除操作)https://www.googleapis.com/auth/keep.readonly:只读权限
你之前找不到可能是配置时未正确搜索或选择,这两个范围是官方支持的。
3. OAuth授权流程的代码实现
使用google-auth-oauthlib库完成本地授权,替换原来的服务账号代码:
from googleapiclient.discovery import build from google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request import pickle import os SCOPES = ['https://www.googleapis.com/auth/keep'] CLIENT_SECRET_FILE = 'client_secret.json' TOKEN_FILE = 'token.pickle' def get_credentials(): creds = None # 读取已保存的token if os.path.exists(TOKEN_FILE): with open(TOKEN_FILE, 'rb') as token: creds = pickle.load(token) # 没有有效凭证则重新授权 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( CLIENT_SECRET_FILE, SCOPES) creds = flow.run_local_server(port=0) # 保存token供下次使用 with open(TOKEN_FILE, 'wb') as token: pickle.dump(creds, token) return creds # 初始化服务并调用API service = build("keep", "v1", credentials=get_credentials()) notes = service.notes().list().execute() print(notes)
4. 首次运行注意事项
- 首次执行代码会弹出浏览器,要求你登录个人Google账号并授权应用访问Keep数据
- 若遇到「应用未验证」提示,点击「高级」→「继续访问」(个人使用的测试应用无需提交验证)
内容的提问来源于stack exchange,提问作者wasp256
相关产品推荐
相关产品推荐

