我在我的 Google 驱动器中使用 php 看不到通过 api 创建的文件和文件夹

I can't see the files and folders created via api in my Google Drive using php

我正在尝试使用 google 驱动器 api 创建文件夹。我能够在最后获得文件 ID。但我无法访问 google 驱动器中的文件夹。好像是权限问题?

$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);

是的,似乎是权限问题。尝试删除

'permissions' => array('type'=>'anyone','role'=>'reader','allowFileDiscovery'=>1),

来自

$fileMetadata = new Google_Service_Drive_DriveFile(array( 'name' => 'Invoices', 'permissions' => array('type'=>'anyone','role'=>'reader','allowFileDiscovery'=>1), 'mimeType' => 'application/vnd.google-apps.folder'));

正在创建的文件属于 API 帐户电子邮件(类似于 api-service-account@yourproject.iam.gserviceaccount.com。如果您转到驱动器 URL: https://docs.google.com/document/d/{ID},你会得到一个"Permission Denied, request access"页面。

服务帐户电子邮件与您的用户电子邮件无关。

为了与您的用户一起创建文件,您需要创建一个 OAauth 令牌授权。

  1. 按照 "Step 1: Turn on the Drive API" this page

  2. 上的步骤创建 OAuth 凭据
  3. 修改你的脚本,按照this page上的例子,你可以:


use Google_Client;
use Google_Service_Drive;
use Google_Service_Drive_DriveFile;

...

$file = 'root/to/your/credentials.json';

$client = new Google_Client();
$client->setScopes([
    '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'
]);

$client->setAuthConfig($file);
$authUrl = $client->createAuthUrl();

printf("Open the following link in your browser:\n%s\n", $authUrl);

// You will need to open this url
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));

$accessToken = $client->authenticate($authCode);

file_put_contents($credentialsPath, $accessToken);
printf("Credentials saved to %s\n", $credentialsPath);

$client->setAccessToken(json_decode($accessToken, true));

$service = new Google_Service_Drive($client);

$metadata = new Google_Service_Drive_DriveFile([
    'name' => 'testing drive',
    'mimeType' => "application/vnd.google-apps.document"
]);
$file = $service->files->create($metadata, []);
print_r($file);

该文件应该在您的云端硬盘主目录中。

顺便说一句,我建议您尝试新版本 google/apiclient:2.0.2(仍处于测试阶段,但工作正常)。

Mayrop 的回答部分正确。

正在创建的文件属于 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();
  }

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

您还可以使用

禁用通过电子邮件发送通知
$service->permissions->insert($fileId, $newPermission,array('sendNotificationEmails'=>false));

希望这对你有用。

所以我已经解决了它,并最终在 Php 中为 Google Drive V3 获得了 完整的工作解决方案 。如下:

您可以浏览此要点以获得完整代码并更好地理解: https://gist.github.com/KartikWatts/22dc9eb20980b5b4e041ddcaf04caf9e

首先添加@Hitu Bansal 的回答: 此代码工作正常:

$this->newPermission = new Google_Service_Drive_Permission();
$value=<YOUR EMAIL ADDRESS>;
$type="user";
$role="reader";
$this->newPermission->setEmailAddress($value);
$this->newPermission->setType($type);
$this->newPermission->setRole($role);
$this->newPermission->allowFileDiscovery;

try {
$res= $this->service->permissions->create($folder_id, $this->newPermission);
print_r($res);
catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
}

如果您想与任何人分享 link:您可以使用

    $type="anyone";
    $this->newPermission->setType($type);
    $this->newPermission->setRole($role);
    $this->newPermission->allowFileDiscovery;
    $this->newPermission->view;

注意:

  • 在这种情况下,setEmailAddress() 应被删除。
  • 重要提示:如果是'anyone',该文件夹肯定不会出现在您的个人驱动器中,但是您仍然可以通过使用您已经知道的 folder_id 访问 link 来根据给定的角色访问文件夹并与之交互。因此,您可以按以下方式访问 link:https://drive.google.com/drive/u/5/folders/**{folder_id}** 它现在可以公开访问了。

为了更清楚地参考参数以及描述,这真的很有帮助:https://developers.google.com/drive/api/v3/reference/permissions#methods

注意: 在我个人看来,使用 Google Drive API 的更好方法是首先手动创建文件夹(如果工作流程允许它),然后使用该创建的文件夹的 folder_id 上传文件。 记住,如果文件夹是手动创建的,之前应向服务帐户电子邮件ID提供权限。

虽然权限问题是这个问题的主要原因。在使用服务帐户上传文件夹或文件后,我所做的就是指定父文件夹。如果您在没有父文件夹 ID 的情况下上传/创建文件夹/文件,则该对象的所有者将是您正在使用的服务帐户。
通过指定父ID,它将使用继承的权限
这是我在 php (google/apiclient)

中使用的代码
$driveFile = new Google\Service\Drive\DriveFile();
$driveFile->name = 'New Folder';
$driveFile->mimeType = 'application/vnd.google-apps.folder';
$driveFile->parents = ['123456789qwertyuiop'];
$result = $service->files->create($driveFile);