How to get livestream tiny URL with YouTube Data API v3

本秂侑毒 提交于 2021-01-29 07:36:48

问题


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.

please find the image here


回答1:


Yes, take your channel url and add /live .




回答2:


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.



来源:https://stackoverflow.com/questions/64121427/how-to-get-livestream-tiny-url-with-youtube-data-api-v3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!