YouTube API v3 对用户进行身份验证,然后获取他的视频

YouTube API v3 authenticate user and then get his videos

我是 YouTube 新手 API, 在我的网站上,我允许用户上传他们的视频,我想使用 youtube api 添加一个新功能,允许用户从他们的 youtube 频道上传他们的视频。

场景:

  1. 用户将使用他的 google 帐户登录
  2. 我会得到他的 youtube 用户名或频道 ID
  3. 然后我将从频道 ID 或用户名中获取视频 ID
  4. 那我就列出他的视频,他可以选择他想要的 上传

下面是我的代码,它工作正常,我想要的步骤是与用户进行身份验证并获取他的用户名或频道 ID

$(document).ready(function () {

  var channelName = 'PochicaKing';

  $.get(
          "https://www.googleapis.com/youtube/v3/channels", {
            part: 'contentDetails',
            forUsername: channelName,
//            mine: true,
//            access_token: 'ya29.cQEoJbvUmAMT6xJFgDoi4gq0EW8O5Q_9ZqLtNUay0AbNh-mldmNh5mlicarLx00AHxL_O6Vur8M3Bw',
            key: 'AIzaSyARSqGexKgNt0BdHUJBXg2pnxMGgYXrszw'
          },
  function (data) {

    $.each(data.items, function (i, item) {
      console.log(item);
      pid = item.contentDetails.relatedPlaylists.likes;
      gitVids(pid);
    });

  }
  );

  function gitVids(pid) {

    $.get(
            "https://www.googleapis.com/youtube/v3/playlistItems", {
              part: 'snippet',
              maxResults: 5,
              playlistId: pid,
              key: 'AIzaSyARSqGexKgNt0BdHUJBXg2pnxMGgYXrszw'
            },
    function (data) {

      var output;
      $.each(data.items, function (i, item) {
//      console.log(item);
        vidTitle = item.snippet.title;
        vidId = item.snippet.resourceId.videoId;

        output = '<li><iframe width="420" height="315"src="http://www.youtube.com/embed/' + vidId + '"></iframe></li>'
        $('#results').append(output);
      });

    }
    );



  }





});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<html>
  <head>
    <script src="jquery.min.js" type="text/javascript"></script>
    <script src="script.js" type="text/javascript"></script>
    
  </head>
  <body>
    <ul id="results"></ul>
  </body>
</html>

我找到了,通过这个样本 https://github.com/youtube/api-samples 对于 php

如果您下载了它,它将无法运行,因为它需要 "Src/Google" 中的另一个文件夹 "Google" 这里 https://github.com/google/google-api-php-client/tree/master/src/Google

您还需要在文件中包含 "autoload.php"