Not able to access my google drive Files with service account

google-drive-api, php

Solution

That's because xyz@gmail.com doesn't own or have any access to the file. 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. (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).

If abc@gmail.com has access to the file in the UI, they should have access when authenticated via the API. Can you show your code here?

Problem

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. this user abc@gmail.com should login to application and then he can see and download all the files available in his drive. currently I have used the google drive's service account for this. I am able to upload the file to google drive and I can share it to user 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. 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. 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: ``` 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: ``` 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 ``` 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. What I want is only some files will be shared to abc@gmail.com and he should get only those files.

Original source