问题描述
function authenticate() {
return gapi.auth2.getAuthInstance()
.signIn({scope: "https://www.googleapis.com/auth/youtube.readonly"})
.then(function() { console.log("Sign-in successful"); },function(err) { console.error("Error signing in",err); });
}
function loadClient() {
gapi.client.setApiKey("YOUR_API_KEY");
return gapi.client.load("https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest")
.then(function() { console.log("GAPI client loaded for API"); },function(err) { console.error("Error loading GAPI client for API",err); });
}
// Make sure the client is loaded and sign-in is complete before calling this method.
function execute() {
return gapi.client.youtube.liveStreams.list({
"part": [
"snippet,cdn,contentDetails,status"
//"cdn"
],"mine": true
})
.then(function(response) {
// Handle the results here (response.result has the parsed body).
console.log("Response",response);
var responseData = JSON.stringify(response);
alert(responseData);
//alert(response.result.items);
var itemsArr = response.result.items;
var itemObj = itemsArr[0];
alert('streamName = ' + itemObj.cdn.ingestionInfo.streamName);
//alert(responseData.result);
//var result = responseData.result;
},function(err) { console.error("Execute error",err); });
}
gapi.load("client:auth2",function() {
gapi.auth2.init({client_id: "YOUR_CLIENT_ID"});
});
<script src="https://apis.google.com/js/api.js"></script>
<button onclick="authenticate().then(loadClient)">authorize and load</button>
<button onclick="execute()">execute</button>
我是YouTube实时流媒体的新手。我正在通过我的应用程序进行操作。我已经在此门户网站上浏览了各种问题/答案,但是找不到/理解获取它的方法。
是否可以通过YouTube Data API v3获得实时流微小URL(例如https://youtu.be/OHi8m4o8XeQ
),以便我可以将实时流分享给观众?
我从YouTube Data API v3中获得了一个流键/名称(20个字符的字母数字键,中间有四个-
),我将使用它来流式传输到YouTube。
我正在添加一个屏幕截图以供参考。我想要右上角的小网址(类似https://youtu.be/someid)。
解决方法
是的,获取您的频道网址并添加/live
。
与给定视频相关联的YouTube缩短的URL(由其ID VIDEO_ID
标识)的格式为:
https://youtu.be/VIDEO_ID
,
VIDEO_ID
的位置(通常,尽管未正式记录)遵循以下正则表达式模式:
^[0-9a-zA-Z_-]{11}$
。
对于实时流,为了能够共享您创建的此类流的缩短的URL,应获取与该流相关的视频ID 。
该视频ID将作为id
实时流的LiveBroadcasts
资源的属性is bound to的值来找到。