正在通过 YouTube 更新默认广播的流标题 API
Updating the default broadcast's stream title via YouTube API
使用下面的link,可以轻松更新title
:
https://developers.google.com/youtube/v3/live/docs/liveStreams/update?apix=true
.
我希望在 Google Apps 脚本中使用 YouTube API 实现相同的功能。下面的代码成功运行,但不是更新它而是创建一个新流。
不同的是上面的link不需要scheduledStartTime
。但是 API 需要它。当给出 scheduledStartTime
时,它会创建一个新广播而不是更新原始广播。如果 scheduledStartTime
被赋值为 1970-01-01T00:00:00Z
,它的行为就好像 scheduledStartTime
没有被考虑在内。
要分配什么值以便它更新默认广播流而不是更新广播流。
还是我还缺少其他任何东西来完成更新过程?
这是我的代码:
Logger.log('Starting');
service = getService();
if (service.hasAccess()) {
//Fetch the LiveBroadcast Title and Description Details
var url = "https://www.googleapis.com/youtube/v3/liveBroadcasts?broadcastStatus=upcoming&broadcastType=all";
var parameters = {'headers' : {'Content-Type': 'application/json', 'Authorization': 'Bearer ' + service.getAccessToken()}};
var response = UrlFetchApp.fetch(url,parameters);
var result = JSON.parse(response.getContentText());
//Logger.log(result);
var resource = result.items[0];
//Edit the LiveBroadcast Title and Description Details scheduledStartTime
var title = "as per today";
var description = "new desc check";
var scheduledStartTime ='1970-01-01T00:00:00Z';
var privacyStatus = 'public';
//var privacyStatus = 'unlisted';
//var scheduledStartTime = '2020-10-26T04:08:00Z';
data = {
'id' : 'MDaEvnSioI',
'status' : { 'privacyStatus' : 'public' , 'selfDeclaredMadeForKids' : false},
'snippet' : { 'title' : title , 'description' : description , 'scheduledStartTime' : scheduledStartTime }
};
//Update the LiveBroadcast
Logger.log("--");
var jsondata = JSON.stringify(data);
Logger.log(jsondata);
var options = {
'headers': {
'Authorization': 'Bearer ' + service.getAccessToken()
},
'contentType': 'application/json',
'method' : 'post',
'payload': jsondata,
'muteHttpExceptions':true
};
var response = UrlFetchApp.fetch(url2, options);
Logger.log(response.getContentText());
Logger.log('service has access...!!');
如果您只更新广播的 title
和 description
属性,那么根据官方文档,您可以简单地使用 Videos.update
API 端点:
snippet.title
(string)
The broadcast's title. Note that the broadcast represents exactly one YouTube video. You can set this field by modifying the broadcast resource or by setting the title
field of the corresponding video resource.
snippet.description
(string)
The broadcast's description. As with the title
, you can set this field by modifying the broadcast resource or by setting the description
field of the corresponding video resource.
为此,请按如下方式修改您的代码:
// First step: obtain the video's 'categoryId'
var url = "https://www.googleapis.com/youtube/v3/videos?part=id,snippet&fields=items/snippet/categoryId&id=MDaEvnSioI";
var options = {
'headers': {
'Authorization': 'Bearer ' + service.getAccessToken()
}
};
var response = UrlFetchApp.fetch(url, options);
var result = JSON.parse(response.getContentText());
var categoryId = result.items[0].snippet.categoryId;
// Second step: update the video's 'title' and 'description',
// using its 'categoryId' obtained above
var url2 = "https://www.googleapis.com/youtube/v3/videos?part=id,snippet";
var data2 = {
'id': 'MDaEvnSioI',
'snippet': {
'title': title,
'description': description,
'categoryId': categoryId
}
};
var options2 = {
'headers': {
'Authorization': 'Bearer ' + service.getAccessToken()
},
'contentType': 'application/json',
'payload': JSON.stringify(data2),
'method': 'put'
};
var response2 = UrlFetchApp.fetch(url2, options2);
请注意,更新视频的 title
和 description
是一个 two-step 过程,因为根据官方文档,在调用 Videos.update
端点时视频的 snippet
对象,one need to pass that video's categoryId
属性 也到端点。
因此,首先使用 Videos.list
API 端点查询视频的当前 categoryId
属性,然后才调用 Videos.update
.
另请注意,上面的代码已大大简化,因为它不处理这些 API 调用可能返回的错误。
关于您的问题(引用):
[...] And when scheduledStartTime is given, it creates a new broadcast rather than updating the original one.
请确认在 URL 上调用 API:
https://www.googleapis.com/youtube/v3/liveBroadcasts
当您处理代码时,您实际上是在调用 LiveBroadcasts.insert
API endpoint instead of the LiveBroadcasts.update
,正如您实际期望的那样。
这恰好是因为这两个 API 端点具有相同的 URL;区分两者的是调用每个方法的 HTTP 方法:前者为 POST
,后者为 PUT
。
由于您对 UrlFetchApp.fetch
的第二次调用确实明确指定 method
为 POST
,您实际上是在调用 LiveBroadcasts.insert
端点而不是 LiveBroadcasts.update
.
使用下面的link,可以轻松更新title
:
https://developers.google.com/youtube/v3/live/docs/liveStreams/update?apix=true
.
我希望在 Google Apps 脚本中使用 YouTube API 实现相同的功能。下面的代码成功运行,但不是更新它而是创建一个新流。
不同的是上面的link不需要scheduledStartTime
。但是 API 需要它。当给出 scheduledStartTime
时,它会创建一个新广播而不是更新原始广播。如果 scheduledStartTime
被赋值为 1970-01-01T00:00:00Z
,它的行为就好像 scheduledStartTime
没有被考虑在内。
要分配什么值以便它更新默认广播流而不是更新广播流。 还是我还缺少其他任何东西来完成更新过程?
这是我的代码:
Logger.log('Starting');
service = getService();
if (service.hasAccess()) {
//Fetch the LiveBroadcast Title and Description Details
var url = "https://www.googleapis.com/youtube/v3/liveBroadcasts?broadcastStatus=upcoming&broadcastType=all";
var parameters = {'headers' : {'Content-Type': 'application/json', 'Authorization': 'Bearer ' + service.getAccessToken()}};
var response = UrlFetchApp.fetch(url,parameters);
var result = JSON.parse(response.getContentText());
//Logger.log(result);
var resource = result.items[0];
//Edit the LiveBroadcast Title and Description Details scheduledStartTime
var title = "as per today";
var description = "new desc check";
var scheduledStartTime ='1970-01-01T00:00:00Z';
var privacyStatus = 'public';
//var privacyStatus = 'unlisted';
//var scheduledStartTime = '2020-10-26T04:08:00Z';
data = {
'id' : 'MDaEvnSioI',
'status' : { 'privacyStatus' : 'public' , 'selfDeclaredMadeForKids' : false},
'snippet' : { 'title' : title , 'description' : description , 'scheduledStartTime' : scheduledStartTime }
};
//Update the LiveBroadcast
Logger.log("--");
var jsondata = JSON.stringify(data);
Logger.log(jsondata);
var options = {
'headers': {
'Authorization': 'Bearer ' + service.getAccessToken()
},
'contentType': 'application/json',
'method' : 'post',
'payload': jsondata,
'muteHttpExceptions':true
};
var response = UrlFetchApp.fetch(url2, options);
Logger.log(response.getContentText());
Logger.log('service has access...!!');
如果您只更新广播的 title
和 description
属性,那么根据官方文档,您可以简单地使用 Videos.update
API 端点:
snippet.title
(string)
The broadcast's title. Note that the broadcast represents exactly one YouTube video. You can set this field by modifying the broadcast resource or by setting thetitle
field of the corresponding video resource.
snippet.description
(string)
The broadcast's description. As with thetitle
, you can set this field by modifying the broadcast resource or by setting thedescription
field of the corresponding video resource.
为此,请按如下方式修改您的代码:
// First step: obtain the video's 'categoryId'
var url = "https://www.googleapis.com/youtube/v3/videos?part=id,snippet&fields=items/snippet/categoryId&id=MDaEvnSioI";
var options = {
'headers': {
'Authorization': 'Bearer ' + service.getAccessToken()
}
};
var response = UrlFetchApp.fetch(url, options);
var result = JSON.parse(response.getContentText());
var categoryId = result.items[0].snippet.categoryId;
// Second step: update the video's 'title' and 'description',
// using its 'categoryId' obtained above
var url2 = "https://www.googleapis.com/youtube/v3/videos?part=id,snippet";
var data2 = {
'id': 'MDaEvnSioI',
'snippet': {
'title': title,
'description': description,
'categoryId': categoryId
}
};
var options2 = {
'headers': {
'Authorization': 'Bearer ' + service.getAccessToken()
},
'contentType': 'application/json',
'payload': JSON.stringify(data2),
'method': 'put'
};
var response2 = UrlFetchApp.fetch(url2, options2);
请注意,更新视频的 title
和 description
是一个 two-step 过程,因为根据官方文档,在调用 Videos.update
端点时视频的 snippet
对象,one need to pass that video's categoryId
属性 也到端点。
因此,首先使用 Videos.list
API 端点查询视频的当前 categoryId
属性,然后才调用 Videos.update
.
另请注意,上面的代码已大大简化,因为它不处理这些 API 调用可能返回的错误。
关于您的问题(引用):
[...] And when scheduledStartTime is given, it creates a new broadcast rather than updating the original one.
请确认在 URL 上调用 API:
https://www.googleapis.com/youtube/v3/liveBroadcasts
当您处理代码时,您实际上是在调用 LiveBroadcasts.insert
API endpoint instead of the LiveBroadcasts.update
,正如您实际期望的那样。
这恰好是因为这两个 API 端点具有相同的 URL;区分两者的是调用每个方法的 HTTP 方法:前者为 POST
,后者为 PUT
。
由于您对 UrlFetchApp.fetch
的第二次调用确实明确指定 method
为 POST
,您实际上是在调用 LiveBroadcasts.insert
端点而不是 LiveBroadcasts.update
.