我可以';我看不到在我的Google Drive中使用php通过api创建的文件和文件夹


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

我正在尝试使用谷歌驱动器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);

正在创建的文件属于API帐户电子邮件(类似于api-service-account@yourproject.iam.gserviceaccount.com。如果转到驱动器URL:https://docs.google.com/document/d/{ID},您将得到一个"权限被拒绝,请求访问"页面。

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

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

  1. 按照本页上的步骤"步骤1:打开驱动器API"创建OAuth凭据

  2. 修改你的脚本,按照这个页面上的例子,你可以有:


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

希望这对你有用。

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

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

所以我对它进行了研究,最终在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();
}

相反,如果你想与任何人共享链接:你可以使用

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

注意:

  • 在这种情况下,应删除setEmailAddress()
  • 重要信息:如果出现'yone',该文件夹肯定不会在您的个人驱动器中显示,但您仍然可以通过使用您已经知道的folder_id访问链接,按照给定的角色访问该文件夹并与之交互。因此,您可以访问以下链接: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);