如何通过API获取YouTube账号的未读用户通知?
通过YouTube Data API获取未读通知并导出列表
前置准备
- 前往Google Cloud Console创建项目,启用YouTube Data API v3
- 创建OAuth 2.0客户端ID(选择「桌面应用」类型,适合个人自用场景)
- 下载授权凭证JSON文件(命名为
client_secret.json即可)
用Python脚本批量获取并导出(终端用户快速实现)
- 安装依赖包
pip install google-api-python-client google-auth-httplib2 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.path import csv # 仅申请通知读取权限,最小权限原则 SCOPES = ['https://www.googleapis.com/auth/youtube.readonly'] def main(): creds = None # 保存授权凭证,避免重复登录 if os.path.exists('token.pickle'): with open('token.pickle', '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.json', SCOPES) creds = flow.run_local_server(port=0) with open('token.pickle', 'wb') as token: pickle.dump(creds, token) # 构建API客户端 youtube = build('youtube', 'v3', credentials=creds) next_page_token = None all_notifications = [] # 分页拉取所有未读视频通知 while True: request = youtube.notifications().list( part='snippet', maxResults=50, # API单次返回上限 pageToken=next_page_token, type='video', # 仅筛选视频类通知,可改为'all'包含所有类型 unread=True ) response = request.execute() # 提取关键信息 for item in response['items']: snippet = item['snippet'] all_notifications.append({ '视频标题': snippet['title'], '频道名称': snippet['channelTitle'], '通知时间': snippet['publishedAt'], '视频链接': f'https://www.youtube.com/watch?v={snippet["resourceId"]["videoId"]}' }) next_page_token = response.get('nextPageToken') if not next_page_token: break # 导出为CSV文件 with open('youtube_unread_notifications.csv', 'w', newline='', encoding='utf-8-sig') as f: writer = csv.DictWriter(f, fieldnames=all_notifications[0].keys()) writer.writeheader() writer.writerows(all_notifications) print(f"已导出{len(all_notifications)}条未读通知到youtube_unread_notifications.csv") if __name__ == '__main__': main()
- 首次运行会弹出浏览器,登录你的YouTube账号完成授权,之后脚本会自动拉取所有未读通知并保存为CSV文件。
关键注意事项
- API配额:YouTube Data API v3免费配额为每日10000单位,
notifications.list单次调用消耗1单位,拉取数千条通知完全在配额范围内。 - 通知状态:该脚本仅读取通知,不会将未读标记为已读;若需要标记已读,需改用
https://www.googleapis.com/auth/youtube权限,并调用notifications.markAsRead接口。 - 通知类型:可修改
type参数,比如comment(评论通知)、live(直播提醒)或all(所有类型)。
无代码替代方案(Google Sheets)
- 打开Google Sheets,点击「扩展」→「Apps Script」
- 粘贴以下脚本:
function exportYouTubeNotifications() { var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); var headers = ['视频标题', '频道名称', '通知时间', '视频链接']; sheet.clear(); sheet.appendRow(headers); var nextPageToken = null; do { var response = YouTube.Notification.list('snippet', { maxResults: 50, pageToken: nextPageToken, type: 'video', unread: true }); response.items.forEach(item => { var snippet = item.snippet; sheet.appendRow([ snippet.title, snippet.channelTitle, new Date(snippet.publishedAt), `https://www.youtube.com/watch?v=${snippet.resourceId.videoId}` ]); }); nextPageToken = response.nextPageToken; } while (nextPageToken); SpreadsheetApp.getUi().alert('通知导出完成!'); }
- 点击「运行」,完成授权后,未读通知会自动导入到当前Sheet中。
内容的提问来源于stack exchange,提问作者nkross1212211211
相关产品推荐
相关产品推荐

