I’m trying to get a video from my google drive account and publish it on my website. The idea is to authorize the access to the file using a service account, so the video will be “public” accessible without the user using his google credentials.
Right now for the images I download them and the show it from my server, but due to storage space I would prefer not doing the same for videos
Here’s my code:
$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 ImageCacheImageCache(); $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>';
right now I tried to get the webContentLink and then put the url I get as source for a video tag, but I get a 403 error, so still I didn’t authorize the access using the service account
Any help would be appreciated
Advertisement
Answer
Embedding the webContentLink
to your website won’t make this publicly available. The webContentLink
is as restricted as the file itself: it can only be accessed by users with which the file has been shared.
So you should do one of these:
- Make this video public (via Permissions: create, or through the UI itself) (
role: reader
andtype: anyone
). - Download it serve it from your server, as with your images.