You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否通过People API上传头像?Python脚本实现疑问

关于People API实现头像上传的问题

不行,Google People API不支持上传或更新用户头像的操作,它的核心能力集中在读取、管理联系人信息以及个人基础资料的部分字段,头像相关的写入操作不在其功能覆盖范围内。

如果你的需求是给Google账号上传头像,分两种情况处理:

  • 若是G Suite/Google Workspace的企业账号,可以用Admin SDK的Directory API,通过users.update接口,将头像图片转为base64编码后传入请求体完成上传。
  • 若是普通个人Google账号,目前没有公开的官方API支持主动上传头像,用户只能通过Google账号设置页面手动修改。

给你一段适用于Workspace账号的Python示例代码(需提前完成API授权配置):

from googleapiclient.discovery import build
from google.oauth2 import service_account
import base64

# 加载服务账号凭证
SERVICE_ACCOUNT_FILE = 'your-service-account-key.json'
SCOPES = ['https://www.googleapis.com/auth/admin.directory.user']

credentials = service_account.Credentials.from_service_account_file(
    SERVICE_ACCOUNT_FILE, scopes=SCOPES)
# 指定拥有权限的管理员邮箱
delegated_credentials = credentials.with_subject('admin@your-domain.com')

service = build('admin', 'directory_v1', credentials=delegated_credentials)

# 读取本地头像图片并转为base64编码
with open('avatar.jpg', 'rb') as img_file:
    base64_image = base64.b64encode(img_file.read()).decode('utf-8')

# 发起更新用户头像的请求
user_key = 'target-user@your-domain.com'
body = {
    'photoData': base64_image
}

response = service.users().update(userKey=user_key, body=body).execute()
print(f"头像更新成功,目标用户:{response['name']['fullName']}")

内容的提问来源于stack exchange,提问作者Michael Krezalek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 02:05:35