如何使用 YouTube Data API v3 获取直播小 URL [英] How to get livestream tiny URL with YouTube Data API v3

查看:37
本文介绍了如何使用 YouTube Data API v3 获取直播小 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function authenticate() {返回 gapi.auth2.getAuthInstance().signIn({scope: "https://www.googleapis.com/auth/youtube.readonly"}).then(function() { console.log("登录成功"); },function(err) { console.error("登录时出错", err);});}函数加载客户端(){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 客户端为 API 加载"); },function(err) { console.error("为 API 加载 GAPI 客户端时出错", err);});}//在调用此方法之前,请确保客户端已加载且登录已完成.函数执行(){返回 gapi.client.youtube.liveStreams.list({部分": [片段,cdn,内容详细信息,状态"//"cdn"],我的":真的}).then(功能(响应){//在这里处理结果(response.result 有解析的主体).console.log("响应", 响应);var responseData = JSON.stringify(response);警报(响应数据);//警报(响应.结果.项目);var itemsArr = response.result.items;var itemObj = itemsArr[0];alert('streamName = ' + itemObj.cdn.ingestionInfo.streamName);//警报(响应数据.结果);//var 结果 = responseData.result;},function(err) { console.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)">授权和加载</button><button onclick="execute()">execute</button>

我是 YouTube 直播的新手.我是通过我的应用程序来做的.我在此门户网站上浏览了各种问题/答案,但无法找到/理解获取它的方法.

有什么方法可以(使用 YouTube Data API v3)获取直播小 URL(类似于 https://youtu.be/OHi8m4o8XeQ),以便我可以将直播分享给我的观众?

我从 YouTube Data API v3 获得了一个流密钥/名称(20 个字符的字母数字密钥,中间有四个 -),我将用它来流式传输到 YouTube.

我正在添加一张屏幕截图以供参考.我想要右上角的小网址(类似于 的 LiveBroadcasts 已绑定资源 您的直播.

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>

I am new to YouTube live streaming. I am doing it through my application. I have gone through various question/answers on this portal but couldn't find/understand a way to get it.

Is there any way (with YouTube Data API v3) to get live stream tiny URL (something like https://youtu.be/OHi8m4o8XeQ) so that I can share my live stream to my audiences?

I have got a stream key/name (20 character alphanumeric key with four - in between) from YouTube Data API v3, that I will use to stream to YouTube.

I am adding one screenshot for reference. I want the tiny url (something like https://youtu.be/someid) in upper right side.

解决方案

YouTube's shortened URL associated to a given video -- identified by its ID VIDEO_ID -- is of form:

https://youtu.be/VIDEO_ID,

where (usually, though not officially documented as such) VIDEO_ID obeys to the following regex pattern:

^[0-9a-zA-Z_-]{11}$.

In the case of live streaming, for to be able to share the shortened URL of one such stream you've created, you should obtain the video ID associated to that stream.

That video ID is to be found as the value of the property id of the LiveBroadcasts resource that is bound to your live stream.

这篇关于如何使用 YouTube Data API v3 获取直播小 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆