YouTube 身份验证
YouTube Authentication
我正在尝试在 YouTube 上使用“Google Apps 脚本”做两件事;
(a) 案例一
- 要求:对于我的每个播放列表,将播放列表的详细信息发送到
Google Sheet
- 方法:使用代码如下 “PlaylistItems:
list” 方法使用数组添加了我的播放列表 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);
- 结果:我的所有“Public”播放列表都按预期工作。
(b) 案例二
- 要求:获取我所有播放列表的列表到 Google Sheet
- 方法:使用与上述 (a) 类似概念的代码,使用 “Playlists: list” 方法。
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 call to youtube.playlists.list failed
with error: Channel not found. (line 9, file ")
我在下面用“试试这个 API” 尝试了这两种情况,并按预期工作:
- https://developers.google.com/youtube/v3/docs/playlists/list
- https://developers.google.com/youtube/v3/docs/playlistItems/list
我认为对于(案例 II),我的 Apps 脚本中可能缺少对 API 请求的授权?可能使用 API-Key 或 Auth2.0?
(当我尝试使用“[试试这个 API][1]”时,我在代码示例中注意到它们有授权步骤。
问题:如何在脚本中添加授权(auth2或API-Key)步骤?
感谢帮助
如果您像您所说的那样使用 Apps 脚本。没有必要验证您的操作方式。我什至会说 Apps Script 的要点之一是将所有 "boring" 东西都拿走。
只需继续并在 Apps 脚本编辑器中启用 Youtube 服务(资源 > 高级 Google 服务)。
之后您就可以使用 App Script Youtube Service 中的任何示例:
/**
* 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 not Authorising a Google+ Page's YouTube Channel",这是一个问题。这是一个已知问题,已在以下主题中讨论过:
所以我修改了上面的“(b) 案例 II - 要求”如下,它奏效了:
var my_pl = YouTube.Playlists.list("snippet,contentDetails",{"maxResults": 50, channelId: '**xxxxxxxxx**' })
我正在尝试在 YouTube 上使用“Google Apps 脚本”做两件事;
(a) 案例一 - 要求:对于我的每个播放列表,将播放列表的详细信息发送到 Google Sheet - 方法:使用代码如下 “PlaylistItems: list” 方法使用数组添加了我的播放列表 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);
- 结果:我的所有“Public”播放列表都按预期工作。
(b) 案例二 - 要求:获取我所有播放列表的列表到 Google Sheet - 方法:使用与上述 (a) 类似概念的代码,使用 “Playlists: list” 方法。
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 call to youtube.playlists.list failed with error: Channel not found. (line 9, file ")
我在下面用“试试这个 API” 尝试了这两种情况,并按预期工作:
- https://developers.google.com/youtube/v3/docs/playlists/list
- https://developers.google.com/youtube/v3/docs/playlistItems/list
我认为对于(案例 II),我的 Apps 脚本中可能缺少对 API 请求的授权?可能使用 API-Key 或 Auth2.0? (当我尝试使用“[试试这个 API][1]”时,我在代码示例中注意到它们有授权步骤。
问题:如何在脚本中添加授权(auth2或API-Key)步骤?
感谢帮助
如果您像您所说的那样使用 Apps 脚本。没有必要验证您的操作方式。我什至会说 Apps Script 的要点之一是将所有 "boring" 东西都拿走。
只需继续并在 Apps 脚本编辑器中启用 Youtube 服务(资源 > 高级 Google 服务)。
之后您就可以使用 App Script Youtube Service 中的任何示例:
/**
* 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 not Authorising a Google+ Page's YouTube Channel",这是一个问题。这是一个已知问题,已在以下主题中讨论过:
所以我修改了上面的“(b) 案例 II - 要求”如下,它奏效了:
var my_pl = YouTube.Playlists.list("snippet,contentDetails",{"maxResults": 50, channelId: '**xxxxxxxxx**' })