1
votes

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.

  1. 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.

  2. 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.

1
See my answer below. Even though a user account created the service account, that does not give the user account any special rights to the service accounts files. Depending on your goals here, you may be over-complicating things by using service accounts in the first place, you could just authorize as the actual users. - Jay Lee
can you tell me that how can i authorize the actual user. I did google but no success. - user3247719

1 Answers

4
votes
  1. That's because [email protected] 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 [email protected] so they can see the file but you never gave [email protected] access to the file. (The fact that [email protected] created the Service account does not matter here, the file is explicitly owned by the service account, not [email protected]).

  2. If [email protected] has access to the file in the UI, they should have access when authenticated via the API. Can you show your code here?