Ankita Kashyap
Ankita Kashyap

Reputation: 517

How to embed google drive file using api?

I am working on google drive api intergration. I got fileId and i got all parents and child folder.

But now i want to show my file into web. here is code to get all folder and subfolder and files

$creds = new Google_Auth_AssertionCredentials(
    $serviceAccountName,
    $scopes,
    file_get_contents($keyFile)
);
$client = new Google_Client();
$client->setApplicationName($appName);
$client->setClientId($clientId);
$client->setAssertionCredentials($creds);
$service = new Google_Service_Drive($client);
$optParams = array();
$results = $service->files->listFiles($optParams);

Any idea how to read the file and display on the web.

Upvotes: 0

Views: 500

Answers (1)

Michael Malov
Michael Malov

Reputation: 1887

Add this code to yours

$files = $results->getItems();
foreach($files as $file) {
    try {
        $file = $service->files->get($file["id"]);
        print "Title: " . $file->getTitle() . '</br>';
        //print "Description: " . $file->getDescription();
        //print "MIME type: " . $file->getMimeType();
    } catch (Exception $e) {
        print "An error occurred: " . $e->getMessage();
    }
    $downloadUrl = $file->getDownloadUrl();
    if ($downloadUrl) {
        $request = new Google_Http_Request($downloadUrl, 'GET', null, null);
        $httpRequest = $service->getClient()->getAuth()->authenticatedRequest($request);
        if ($httpRequest->getResponseHttpCode() == 200) {
            var_dump( $httpRequest->getResponseBody() );
        } else {
            // An error occurred.
            print "Error";
        }
    } else {
        // The file doesn't have any content stored on Drive.
        print "No content";
    }

}

Upvotes: 0

Related Questions