我想在YouTube上使用 "Google Apps Script "做两件事。
(a) 案例一--需求。对于我的每一个播放列表,得到播放列表的详细信息到谷歌表 - 方法:使用下面的代码与 "播放列表项目:列表" 方法使用数组添加了我的PLAYLIST ID(手动添加到数组中的播放列表ID)。
var cur_pl = YouTube.PlaylistItems.list("snippet,contentDetails", {"maxResults": 50,"playlistId":MY PLAYLIST ID }) ;
var modRes1 = cur_pl.items.map(function(v) {return [v.snippet.title,,v.snippet.position, v.etag];});
Logger.log(modRes5);
(b) 案例二--需求。方法:使用类似于上面(a)的概念,使用下面的代码来获取我所有的播放列表到一个Google Sheet。"播放列表:列表" 方法。
var my_pl = YouTube.Playlists.list("snippet,contentDetails",{"maxResults": 50,"mine": true })
var modRes2 = my_pl.items.map(function(v) {return [v.snippet.title,v.snippet.channelId]});
Logger.log(modRes2);
GoogleJsonResponseException: API调用youtube.playlists.list失败,出现错误。未找到频道。(第9行,文件")
我尝试了以下两种情况下的 "Try this API",结果和预期的一样:-。https:/developers.google.comyoutubev3docsplaylistslist。 - https:/developers.google.comyoutubev3docsplaylistItemslist。
我想对于(情况二),我可能在我的Apps Script里面缺少对API请求的授权?可能是使用API-Key或Auth2.0?(当我尝试用"[Try this API][1]"时,我注意到在代码示例中,他们有授权步骤)。
问题:如何添加授权(auth2.0)?如何在脚本中添加授权(Auth2或API-Key)步骤?
感谢帮助
如果你使用的是你说的Apps Script。有没有点认证的方式,你正在做的。我甚至会说,Apps Script的一个要点就是把所有 "无聊 "的东西去掉。
只要在Apps Script编辑器里面启用Youtube服务即可(资源> 高级谷歌服务).
之后,你可以使用任何一个样品在 应用脚本Youtube服务:
/**
* Searches for videos about dogs, then logs the video IDs and title.
* Note that this sample limits the results to 25. To return more
* results, pass additional parameters as shown in the YouTube Data API docs.
* @see https://developers.google.com/youtube/v3/docs/search/list
*/
function searchByKeyword() {
var results = YouTube.Search.list('id,snippet', {
q: 'dogs',
maxResults: 25
});
results.items.forEach(function(item) {
Logger.log('[%s] Title: %s', item.id.videoId, item.snippet.title);
});
}
这只是一个例子,当然你可以使用API的任何端点,范围和认证将被处理。
这原来是一个问题,由于 "YouTube API不授权Google+页面的YouTube频道"。这是一个已知的问题,并且已经在下面的帖子中讨论过。
所以我把上面的'(b)情况二-要求'修改成了下面的样子,然后就成功了。
var my_pl = YouTube.Playlists.list("snippet,contentDetails",{"maxResults": 50, channelId: '**xxxxxxxxx**' })