How to configure app.yaml to load google-api-php-c

2019-04-02 15:23发布

I have been trying to get the google api php client to work on app engine. After reading the app.yaml configuration resources provided by the documentation, I have still not been able to get this to work. My app.yaml has the google-api-php-client as it follows:

- url: /google-api-php-client/(.*?)/(.*?)/(.*)
  script: google-api-php-client/\3/\2/\1.php

And, the structure of the folder is:

google-api-php-client->(some_folders)->(some_folders+files)->(some_files)
|      level1       |  |   level2   |  |       level3     |  |  level4  |

I would like to have the configuration set so that I would be able to access the files in level 3 and level 4 and be able to do require_once calls just like these:

require_once ('/google-api-php-client/src/Google_Client.php');
require_once ('/google-api-php-client/src/contrib/Google_DriveService.php');

I am able to do these require_once calls on localhost but not when I deploy the files to app engine. The logs on app engine dashboard show this:

PHP Fatal error: require_once(): Failed opening required '/google-api-php-client/src/Google_Client.php' (include_path='.;/base/data/home/apps/s~...

I would appreciate any and all input. Thanks!

EDIT: I am adding the code where I'm using require_once

require_once ('google-api-php-client/src/Google_Client.php');
require_once ('google-api-php-client/src/contrib/Google_DriveService.php');
session_start();

$client = new Google_Client();
$client->setApplicationName("Drive Demo");
$client->setClientId('****');
$client->setClientSecret('****');
$client->setRedirectUri('http://localhost:8080');
$client->setDeveloperKey('****');
$client->setScopes(array(
  'https://www.googleapis.com/auth/drive',
  'https://www.googleapis.com/auth/userinfo.email',
  'https://www.googleapis.com/auth/userinfo.profile'));
$client->setAccessType('offline');
$service = new Google_DriveService($client);
$fileId = '****';

function printFile($service, $fileId) {
  try {
    $file = $service->files->get($fileId);
    print_r($file);
    print "Title: " . $file->getTitle();
    print "Description: " . $file->getDescription();
    print "MIME type: " . $file->getMimeType();
  } catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
  }
}

function retrieveAllFiles($service) {
  $result = array();
  $pageToken = NULL;

  do {
    try {
      $parameters = array('q'=>'', 'maxResults'=> '25', 'fields'=>'items(title,description,mimeType)');
      if ($pageToken) {
        $parameters['pageToken'] = $pageToken;
      }
      $files = $service->files->listFiles($parameters);

      $result = array_merge($result, $files->getItems());
      $pageToken = $files->getNextPageToken();
    } catch (Exception $e) {
      print "An error occurred: " . $e->getMessage();
      $pageToken = NULL;
    }
  } while ($pageToken);
  return $result;
}

if (!isset($_REQUEST['code']) && !isset($_SESSION['access_token'])) {
    $authUrl = $client->createAuthUrl();
    print("<a href='".$authUrl."'>Authorize me</a>");
}else {
    if (isset($_GET['code'])) {
      $client->authenticate($_GET['code']);
      $_SESSION['access_token'] = $client->getAccessToken();
    }

    if (isset($_SESSION['access_token'])) {
      $client->setAccessToken($_SESSION['access_token']);
    }
    if ($client->getAccessToken()) {
      $_SESSION['access_token'] = $client->getAccessToken();  

    } 

    $files = retrieveAllFiles($service);
    foreach($files as $file){
        //print_r($file);
        print "Title: " . $file->getTitle().'<br/>';
        print "Description: " . $file->getDescription().'<br/>';
        print "MIME type: " . $file->getMimeType().'<br/>';
    }

}   

Update: Removed the '/' as per @Stuart's comment, which solved the issue.

1条回答
冷血范
2楼-- · 2019-04-02 16:10

You don't need to configure app.yaml in this case - as you don't want to route any requests directly to these scripts.

Just put the source code for google-api-php-client in the same directory as your app and deploy it using appcfg.py.

Check my article on configuring the client here.

查看更多
登录 后发表回答