我在使用PHP的Google云端硬盘中看不到通过api创建的文件和文件夹
我正在尝试使用Google Drive API创建文件夹.我可以在最后获取文件ID.但我无法将我的文件夹放在Google驱动器中.似乎有权限问题?
I am trying to create folder using google drive api. I am able to get file id at the end. but i am not able to my folder in google drive. it seems some permission issue?
$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);
Mayrop的答案部分正确.
Mayrop answer is partially right.
正在创建的文件属于API帐户电子邮件(类似于api-service-account@yourproject.iam.gserviceaccount.com.
The files that are being created belong to the API account email (something like api-service-account@yourproject.iam.gserviceaccount.com.
您需要授予该帐户所有者的权限,例如
You need to give permission to owner of that account like
$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();
}
您将在与我共享中看到该文件夹.您也可以手动添加驱动器.
You will see that folder in Shared with me. You can also add in your drive manually.
您还可以使用
$service->permissions->insert($fileId, $newPermission,array('sendNotificationEmails'=>false));
希望这对您有用.