我正在尝试使用google drive 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);
答案 0 :(得分:3)
正在创建的文件属于API帐户电子邮件(类似api-service-account@yourproject.iam.gserviceaccount.com
。如果您转到驱动器网址:
https://docs.google.com/document/d/{ID}
,您将收到“权限被拒,请求访问权限”页面。
服务帐户电子邮件与您的用户电子邮件无关。
要与您的用户一起创建文件,您需要创建OAauth令牌授权。
按照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(目前仍处于测试阶段,但工作正常)。
答案 1 :(得分: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));
希望这对你有用。
答案 2 :(得分:1)
是的,似乎是权限问题。尝试删除
'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'));
答案 3 :(得分:0)
所以我已经解决了它,最终在 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;
注意:
要更清楚地参考参数和说明,这真的很有帮助:https://developers.google.com/drive/api/v3/reference/permissions#methods
注意:在我个人看来,使用 Google Drive API 的更好方法是先手动创建文件夹(如果工作流程允许),然后使用创建的文件夹 ID 上传文件文件夹。 请记住,如果文件夹是手动创建的,则应先向服务帐户电子邮件 ID 提供权限。