我无法使用php在我的Google云端硬盘中看到通过api创建的文件和文件夹

Ank*_*yap 11 php google-drive-api google-admin-sdk

我正在尝试使用谷歌驱动器api创建文件夹.我能够在最后得到文件ID.但我无法在谷歌驱动器中的文件夹.看来有些许可问题?

$scopes = array(
    'https://www.googleapis.com/auth/drive',
    'https://www.googleapis.com/auth/drive.appdata','https://www.googleapis.com/auth/drive.apps.readonly',
    'https://www.googleapis.com/auth/drive.metadata','https://www.googleapis.com/auth/drive.file',
    'https://www.googleapis.com/auth/drive.readonly','https://www.googleapis.com/auth/drive.photos.readonly'
);
$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);
$fileMetadata = new Google_Service_Drive_DriveFile(array(
  'name' => 'Invoices',
  'permissions' => array('type'=>'anyone','role'=>'reader','allowFileDiscovery'=>1),

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

printf("File ID: %s\n", $file->id);
Run Code Online (Sandbox Code Playgroud)

Hit*_*sal 3

梅罗普的回答部分正确。

正在创建的文件属于 API 帐户电子邮件(例如 api-service-account@yourproject.iam.gserviceaccount.com)。

您需要向该帐户的所有者授予权限,例如

    $newPermission = new Google_Service_Drive_Permission();
$value="email id";
$type="user";
$role="writer";
  $newPermission->setValue($value);
  $newPermission->setType($type);
  $newPermission->setRole($role);
 try {
   $res= $service->permissions->insert($fileId, $newPermission);
    print_r($res); 
  } catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
  }
Run Code Online (Sandbox Code Playgroud)

您将在“与我共享”中看到该文件夹​​。您也可以手动添加驱动器。

您还可以使用禁用通过电子邮件发送通知

$service->permissions->insert($fileId, $newPermission,array('sendNotificationEmails'=>false));
Run Code Online (Sandbox Code Playgroud)

希望这对你有用。