【问题标题】:Show Google Drive video on a website using service account使用服务帐户在网站上显示 Google Drive 视频
【发布时间】:2020-09-17 14:36:08
【问题描述】:

我正在尝试从我的 Google Drive 帐户获取视频并将其发布到我的网站上。 这个想法是授权使用服务帐户访问文件,因此无需用户使用其谷歌凭据即可“公开”访问视频。

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

这是我的代码:

  $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 错误,所以我仍然没有使用服务帐户授权访问

任何帮助将不胜感激

【问题讨论】:

    标签: php google-drive-api service-accounts


    【解决方案1】:

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

    所以你应该做以下其中之一:

    • 公开此视频(通过Permissions: create,或通过用户界面本身)(role: readertype: anyone)。
    • 从你的服务器下载它,就像你的图片一样。

    相关:

    【讨论】:

    • 谢谢!我希望在某些方面我可以使用服务帐户进行身份验证,然后直接从谷歌提供服务,我会去更改权限
    猜你喜欢
    • 2023-02-15
    • 2017-02-09
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多