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

如何通过Google API列出拥有谷歌账户读写权限的第三方应用(Python实现)

列出谷歌账户授权的第三方应用(Python实现)

要获取谷歌账户中拥有读写权限的第三方应用,你需要使用Google OAuth2 API的凭证管理接口,以下是具体实现步骤:

1. 安装依赖库

首先安装所需的Python包:

pip install google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client

2. 准备OAuth2客户端凭证

  1. 登录Google Cloud Console,创建新项目。
  2. 启用「OAuth2 API」(在API库中搜索)。
  3. 创建「桌面应用」类型的OAuth客户端ID,下载生成的client_secret.json文件,放在代码同目录下。

3. Python实现代码

from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build

# 所需权限:获取账户授权应用列表
SCOPES = ['https://www.googleapis.com/auth/authz']

def list_authorized_apps():
    # 初始化OAuth2流程
    flow = InstalledAppFlow.from_client_secrets_file(
        'client_secret.json', SCOPES)
    creds = flow.run_local_server(port=0)

    # 构建OAuth2 API服务实例
    service = build('oauth2', 'v2', credentials=creds)

    # 调用API获取所有已授权的应用
    response = service.tokeninfo().list().execute()
    authorized_apps = response.get('items', [])

    # 筛选并输出拥有读写权限的应用
    print("拥有您谷歌账户读写权限的第三方应用:")
    for app in authorized_apps:
        scopes = app.get('scope', [])
        # 判断是否包含读写相关权限范围
        has_write_access = any('write' in scope or 'modify' in scope or 'upload' in scope for scope in scopes)
        if has_write_access:
            print(f"- 应用名称: {app.get('client_name', '未知')}")
            print(f"  客户端ID: {app.get('client_id')}")
            print(f"  授权范围: {', '.join(scopes[:3])}{'...' if len(scopes) >3 else ''}")
            print("---")

if __name__ == '__main__':
    list_authorized_apps()

关键说明

  • 代码通过authz权限获取账户所有已授权应用,再筛选包含读写相关的权限范围(如drive.file、gmail.send等)。
  • 运行代码后会自动打开浏览器,需登录目标谷歌账户并授权此应用访问权限。
  • 若为Google Workspace企业账户,可改用Admin SDK的directory.users.security权限,调用users.get接口获取更详细的应用授权信息。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 09:55:20