使用服务帐户在网站上显示 Google 驱动视频

Show Google Drive video on a website using service account

我正在尝试从我的 google 云端硬盘帐户获取视频并将其发布到我的网站上。 这个想法是使用服务帐户授权对文件的访问,因此无需用户使用其 google 凭据即可“public”访问视频。

现在我下载图片并从我的服务器上显示,但由于存储空间space我不想对视频做同样的事情

这是我的代码:

  $client = getGoogleClient();  // Specify the CLIENT_ID of the app that accesses the backend
  $service = new Google_Service_Drive($client);
  switch ($type) {
    case 1: //video
      $startPos=strrpos($url['URL'], "file/d")+7;
      if($startPos>7)
      {
        $endPos=strrpos($url['URL'],"/");
        $url=substr($url['URL'],$startPos,$endPos-$startPos); //its the file id
      }
      // Get files from our request
      $file = $service->files->get($url,array("fields"=>"webContentLink"));
      $customData=$file->webContentLink;
      $customclass="hasVideo";
      break;
    case 3: //img
      if(is_null($img))
      {
        //we have to donwload the file and store it temporaly
        //find img id
        $startPos=strrpos($url['URL'], "file/d")+7;
        if($startPos>7)
        {
          $endPos=strrpos($url['URL'],"/");
          $url=substr($url['URL'],$startPos,$endPos-$startPos);
          $content = $service->files->get($url, array("alt" => "media"));
          // Open file handle for output.
          $filePath="./cachedFiles/".uniqid().".jpg";
          $outHandle = fopen($filePath, "w+");
          // Until we have reached the EOF, read 1024 bytes at a time and write to the output file handle.
          while (!$content->getBody()->eof())
            fwrite($outHandle, $content->getBody()->read(1024));
          // Close output file handle.
          fclose($outHandle);
          $connection->runQuery("UPDATE File_Elemento SET cachedFile='".$filePath."', lastCached='".date('Y-m-d H:m:s')."' WHERE ID=".$ID);
        }
        else
          $type=0;
      }
      else
        $filePath=$img;
      require_once('./ImageCache/ImageCache.php');
      $imagecache = new ImageCache\ImageCache();
      $imagecache->cached_image_directory = dirname(__FILE__) . '/cachedImg';
      $filePath = $imagecache->cache( $filePath );
      break;
    default:
      break;
  }
  echo '<a onclick="showDetail(this,\''.$customData.'\')" class="grid-item '.($subject ? $subject : "Generico").' '.($customclass!="" ? $customclass : "").'"><div class="card newsCard">'.($type==3 ? '<img class="lazy-load imgPrev" data-src="'.$filePath.'">' : "").'<h3>'.$school.'</h3><h1>'.$name.'</h1>';
  echo '<div class="prev">'.$subject.'</div><span class="goin mainColor">Visualizza</span></div></a>';

现在我尝试获取 webContentLink,然后将我获取的 url 作为视频标签的来源,但出现 403 错误,所以我仍然没有授权使用该服务进行访问帐号

如有任何帮助,我们将不胜感激

webContentLink 嵌入您的网站不会使此 public 可用。 webContentLink 与文件本身一样受到限制:它只能由共享文件的用户访问。

因此您应该执行以下操作之一:

  • 制作此视频 public(通过 Permissions: create,或通过 UI 本身)(role: readertype: anyone)。
  • 从您的服务器下载它,就像您的图片一样。

相关: