致命错误:类“ Google_AssertionCredentials”

时间:2018-06-19 15:28:32

标签: php oauth google-drive-api

暂时,我不得不放弃在OneDrive中创建文件夹。我切换到Google云端硬盘,并且获得了一些成功。

因此,我试图在Google云端硬盘中创建一个文件夹,但出现主题错误消息。

<?php
  require_once 'vendor/autoload.php';
  require_once 'vendor/google/apiclient/src/Google/Client.php';
  require_once 'vendor/google/apiclient-services/src/Google/Service/Drive.php';

  $client = new Google_Client();
  $client->setClientId('myclientID');
  $client->setClientSecret('myclientSecret');
  $client->setRedirectUri('myredirectURI');
  $client->setScopes(array('https://www.googleapis.com/auth/drive.file'));

  session_start();

  function buildService() {//function for first build up service
    global $DRIVE_SCOPE, $SERVICE_ACCOUNT_EMAIL, $SERVICE_ACCOUNT_PKCS12_FILE_PATH;
    $key = file_get_contents($SERVICE_ACCOUNT_PKCS12_FILE_PATH);
    $auth = new Google_AssertionCredentials($SERVICE_ACCOUNT_EMAIL,array($DRIVE_SCOPE),$key);
    $client = new Google_Client();
    $client->setUseObjects(true);
    $client->setAssertionCredentials($auth);
    return new Google_DriveService($client);
  }

  function createPublicFolder($service, $folderName) {
    $file = new Google_DriveFile();
    $file->setTitle($folderName);
    $file->setMimeType('application/vnd.google-apps.folder');

    $createdFile = $service->files->insert($file, array(
      'mimeType' => 'application/vnd.google-apps.folder',
    ));

    $permission = new Google_Permission();
    $permission->setValue('me');
    $permission->setType('anyone');
    $permission->setRole('writer');

    $service->permissions->insert(
      $createdFile->getId(), $permission);
      return $createdFile;
  }


  $DRIVE_SCOPE = 'https://www.googleapis.com/auth/drive';
  $SERVICE_ACCOUNT_EMAIL = 'my.email@mine.com';
  $SERVICE_ACCOUNT_PKCS12_FILE_PATH = 'main docs dummy-x456d55d8371.p12';

  $service=buildService();
  $folderName='root_test';
  $parent=createPublicFolder($service, $folderName);
?>

这是确切的错误消息:

Fatal error: Class 'Google_AssertionCredentials' not found

该错误使我相信OAuth出了点问题,但是我不确定到底缺少什么。我发现autoload.php可以解决这个问题。

1 个答案:

答案 0 :(得分:1)

这取决于您使用的版本:

  • 对于1.0,您可能正在寻找Google_Auth_AssertionCredentials
  • 对于2.0,该类was removed