I want to create an application which can add files to google drive and share that files to other user's say [email protected]. this user [email protected] 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 [email protected]. but I am facing following issues.
using email [email protected] i registered to drive and created a project but when i creates a file and shares it to user [email protected] using drive API the file is not listing on drive of [email protected] but when I tries to access that through API I can see the files.
for user [email protected] 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 **[email protected]
$CLIENT_ID='xxxxxxxxx.apps.googleusercontent.com';
$SERVICE_ACCOUNT_EMAIL = '[email protected]';
$SERVICE_ACCOUNT_PKCS12_FILE_PATH = 'xxxxxxxxxx-privatekey.p12';**
For user **[email protected]
$CLIENT_ID='yyyyyyyyyyy.apps.googleusercontent.com';
$SERVICE_ACCOUNT_EMAIL = '[email protected]';
$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 [email protected] with Following code:
function insertPermission($service, $fileId, $value='[email protected]', $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 [email protected] 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 [email protected] but if I use the same clientID,account email and private key of [email protected] then I will get all the file. What I want is only some files will be shared to [email protected] and he should get only those files.