[英]Not able to access my google drive Files with service account
I want to create an application which can add files to google drive and share that files to other user's say abc@gmail.com. 我想创建一个可以将文件添加到Google驱动器并将该文件共享到其他用户的abc@gmail.com的应用程序。 this user abc@gmail.com should login to application and then he can see and download all the files available in his drive.
该用户abc@gmail.com应该登录到应用程序,然后他可以查看并下载驱动器中所有可用的文件。 currently I have used the google drive's service account for this.
目前,我已经使用了Google Drive的服务帐户。 I am able to upload the file to google drive and I can share it to user abc@gmail.com.
我可以将文件上传到Google驱动器,并且可以将其共享给用户abc@gmail.com。 but I am facing following issues.
但我面临以下问题。
using email xyz@gmail.com i registered to drive and created a project but when i creates a file and shares it to user abc@gmail.com using drive API the file is not listing on drive of xyz@gmail.com but when I tries to access that through API I can see the files. 我使用电子邮件xyz@gmail.com注册了驱动器并创建了一个项目,但是当我创建一个文件并使用驱动器API将该文件共享给用户abc@gmail.com时,该文件未在xyz@gmail.com的驱动器中列出尝试通过API访问它,我可以看到文件。
for user abc@gmail.com that shared files is available in his drive and I can see that when I access his drive through web but when I tried to access that through API it shows me no file. 对于用户abc@gmail.com,共享文件在他的驱动器中可用,我可以看到当我通过Web访问他的驱动器时,但是当我尝试通过API访问该文件时,则显示没有文件。
Please suggest me that where I am going wrong. 请向我建议我要去哪里错了。 Is it possible for me to do this or not?
我可以这样做吗?
for user **xyz@gmail.com
$CLIENT_ID='xxxxxxxxx.apps.googleusercontent.com';
$SERVICE_ACCOUNT_EMAIL = 'xxxxxxxxxxxx@developer.gserviceaccount.com';
$SERVICE_ACCOUNT_PKCS12_FILE_PATH = 'xxxxxxxxxx-privatekey.p12';**
For user **abc@gmail.com
$CLIENT_ID='yyyyyyyyyyy.apps.googleusercontent.com';
$SERVICE_ACCOUNT_EMAIL = 'yyyyyyyyyyyy@developer.gserviceaccount.com';
$SERVICE_ACCOUNT_PKCS12_FILE_PATH = 'yyyyyyyyyyyy-privatekey.p12';**
Using user xyz@gmail ID I created a project and using his above clientID,account email, Key file created the file: 使用用户xyz @ gmail ID创建了一个项目,并使用他的上述clientID,帐户电子邮件,密钥文件创建了文件:
function insertFile($service, $title, $description, $parentId, $mimeType, $filename) {
$file = new Google_DriveFile();
$file->setTitle($title);
$file->setDescription($description);
$file->setMimeType($mimeType);
// Set the parent folder.
if ($parentId != null) {
$parent = new ParentReference();
$parent->setId($parentId);
$file->setParents(array($parent));
}
try {
$data = file_get_contents($filename);
$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => $mimeType,
'convert' =>TRUE,
)
);
$fileID = $createdFile->getId();
return $fileID;
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
exit();
}
}
Shared This file to user abc@gmail.com with Following code: 使用以下代码将此文件共享给用户abc@gmail.com:
function insertPermission($service, $fileId, $value='abc@gmail.com', $type = 'user', $role = 'writer') {
$newPermission = new Google_Permission();
$newPermission->setValue($value);
$newPermission->setType($type);
$newPermission->setRole($role);
try {
return $service->permissions->insert($fileId, $newPermission);
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
return NULL;
}
Then to check the files for abc@gmail.com I logged in using his clientID,accountEmail and private key file and for retriving the files I used below code 然后检查文件abc@gmail.com,我使用他的clientID,accountEmail和私钥文件登录,并检索以下代码中使用的文件
function retrieveAllFiles($service) {
$result = array();
$pageToken = NULL;
do {
try {
$parameters = array();
if ($pageToken) {
$parameters['pageToken'] = $pageToken;
}
$files = $service->files->listFiles($parameters);
$result = array_merge($result, $files->getItems());
$pageToken = $files->getNextPageToken();
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
$pageToken = NULL;
}
} while ($pageToken);
return $result;
}
What I think that I am using different clientID, accountEmail and private key because of this the files are not listing to abc@gmail.com but if I use the same clientID,account email and private key of xyz@gmail.com then I will get all the file. 我认为我使用的是不同的clientID,accountEmail和私钥,因此文件没有列出到abc@gmail.com,但是如果我使用xyz@gmail.com的相同clientID,account电子邮件和私钥,那么我会获取所有文件。 What I want is only some files will be shared to abc@gmail.com and he should get only those files.
我想要的只是将一些文件共享到abc@gmail.com,他应该只获取那些文件。
That's because xyz@gmail.com doesn't own or have any access to the file. 这是因为xyz@gmail.com不拥有该文件或对此文件没有任何访问权。 The service account created the file and is thus the owner.
服务帐户创建了文件,因此是所有者。 You shared the file with abc@gmail.com so they can see the file but you never gave xyz@gmail.com access to the file.
您与abc@gmail.com共享了文件,以便他们可以看到该文件,但是您从未授予xyz@gmail.com访问该文件的权限。 (The fact that xyz@gmail.com created the Service account does not matter here, the file is explicitly owned by the service account, not xyz@gmail.com).
(xyz@gmail.com创建服务帐户这一事实在这里并不重要,该文件由服务帐户(而非xyz@gmail.com)明确拥有)。
If abc@gmail.com has access to the file in the UI, they should have access when authenticated via the API. 如果abc@gmail.com有权访问用户界面中的文件,则通过API进行身份验证时,他们应该具有访问权限。 Can you show your code here?
您可以在这里显示您的代码吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.