Loading...

Google Drive API: Getting a Drive File By ID and Rendering


I would like to accomplish the following:

  1. Serve embedded G Drive Docs (readonly) in a web application (PHP)
  2. Do so without having to use A) embedding the 'shared' URL in an iframe B) publishing a doc/sheet as HTML
  3. I'm using CodeIgniter, but - as such - any OOP solution will work.

What I have:

  1. I have downloaded / installed the Google API V4 libraries.
  2. I have the API libraries enabled and credentials created via the Developer Console
  3. I have hooked in to the Analytics API without issue.

My issues / Where I'm stuck

  1. iFrame embedding won't work because embedded G Drive iframe content only renders if the respective user is also has an active G Suite / Google Account session within their browser (I am working in a closed system, so I would like all users logged into my app be able to see the respective docs without having to have an active Google / G Suite session)
  2. I can initiate the/a Google Drive API, but I'm not sure what method I need to use to pull/render a doc by ID.

My question: can someone point me in the right direction with respect to what method I need to call to be able to query/render a G Drive doc?

Thanks in advance!

Here's what I have thus far:

/**
 * Initializes an G Drive API V4 service object.
 *
 * @return An authorized G Drive API V4 service object.
 */
public function initializeGoogleDrive()
{

  // Use the developers console and download your service account
  // credentials in JSON format. Place them in this directory or
  // change the key file location if necessary.
  $KEY_FILE_LOCATION = APPPATH. '~path/service-account-credentials.json'; // NOTE: APPPATH is my root folder constant

  // Create and configure a new client object.
  $client = new Google_Client();
  $client->setApplicationName("My G Drive App");
  $client->setAuthConfig($KEY_FILE_LOCATION);
  $client->setScopes(['https://www.googleapis.com/auth/drive']);
  $drivefile = new Google_Service_Drive_DriveFile($client);

  return $drivefile;
}

public function getDriveFile(){

  // Get Drive doc/sheet by ID
  // Render Drive doc/sheet
}
- - Source
comments powered by Disqus