技术求助:如何通过GIPHY API将收藏导入Google Sheets?
将GIPHY私人收藏导入Google Sheets的方法
准备工作
- 注册GIPHY开发者账号,创建新应用,获取Client ID、Client Secret和API密钥。
- 找到你的GIPHY用户ID:打开GIPHY个人主页,URL中的用户名部分就是
user_id(比如https://giphy.com/users/spencer123里的spencer123)。
步骤1:在Google Sheets中配置脚本
打开目标Google Sheets文件,依次点击「工具」→「脚本编辑器」,创建一个新的Google Apps Script项目:
- 添加OAuth2库:点击「资源」→「库」,输入项目ID
1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF,选择最新版本后保存。 - 替换以下代码中的占位符(
YOUR_CLIENT_ID、YOUR_CLIENT_SECRET、YOUR_GIPHY_USER_ID、YOUR_GIPHY_API_KEY):
// 配置GIPHY OAuth授权服务 function getGiphyService() { return OAuth2.createService('GIPHY') .setAuthorizationBaseUrl('https://giphy.com/oauth/authorize') .setTokenUrl('https://giphy.com/oauth/token') .setClientId('YOUR_CLIENT_ID') .setClientSecret('YOUR_CLIENT_SECRET') .setCallbackFunction('authCallback') .setPropertyStore(PropertiesService.getUserProperties()) .setScope('user:read'); } // 授权回调处理 function authCallback(request) { var giphyService = getGiphyService(); var isAuthorized = giphyService.handleCallback(request); return HtmlService.createHtmlOutput(isAuthorized ? '授权成功!请关闭页面返回表格。' : '授权失败,请重试。'); } // 核心导入函数 function importGiphyFavorites() { var service = getGiphyService(); if (!service.hasAccess()) { var authUrl = service.getAuthorizationUrl(); SpreadsheetApp.getUi().alert('需授权访问GIPHY收藏:\n' + authUrl); return; } const userId = 'YOUR_GIPHY_USER_ID'; const apiKey = 'YOUR_GIPHY_API_KEY'; const pageSize = 50; let offset = 0; const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); // 清空表格并写入表头 sheet.clear(); sheet.appendRow(['Gif标题', 'Gif链接', '缩略图链接', '创建时间']); // 分页获取所有收藏 while (true) { const apiUrl = `https://api.giphy.com/v1/gifs/favorites?api_key=${apiKey}&user_id=${userId}&limit=${pageSize}&offset=${offset}`; const response = UrlFetchApp.fetch(apiUrl, { headers: { 'Authorization': `Bearer ${service.getAccessToken()}` } }); const data = JSON.parse(response.getContentText()); if (data.data.length === 0) break; // 无更多内容时终止循环 // 写入单页数据 data.data.forEach(gif => { const title = gif.title; const gifUrl = gif.url; const thumbnail = gif.images.fixed_width.url; const createTime = new Date(gif.create_datetime); sheet.appendRow([title, gifUrl, thumbnail, createTime]); }); offset += pageSize; } SpreadsheetApp.getUi().alert('收藏导入完成!'); }
步骤2:运行导入脚本
- 在脚本编辑器中选择
importGiphyFavorites函数,点击运行按钮。 - 首次运行会触发权限申请,按照提示完成Google和GIPHY的双重授权。
- 授权完成后,脚本会自动将所有GIPHY私人收藏导入当前表格。
注意事项
- GIPHY免费API有请求频次限制,若收藏量极大,可能需要分批次运行脚本。
- 确保GIPHY应用已开启
user:read权限,否则无法读取私人收藏。
内容的提问来源于stack exchange,提问作者user14915635
相关产品推荐
相关产品推荐

