Google Drive API: copy file with service account

时间:2016-04-21 22:16:45

标签: php google-drive-api

EDIT: When copying files that the service account created via the API, the copy goes through, but this error persists and the file id in the error corresponds to the copy. When copying a file the service account does not own (but has edit access to), the copy does not occur and the file id in the error is for the original file.

The service account does not seem to have visibility outside of files that it created via the API, and does not have visibility to copies of those files.

=-=-=-=-=-=-=-=-=-=-=-=-=-

I am getting the same error as this question: Google Drive API copy() - 404 error except my setup is a little different.

Creating files works fine.

I am trying to create documents via copy that will not be owned by users but still give them read / write / no download access. I'll be happy if it just copies for now.

Here is the code:

    $service = GoogleDrive::connect();

    $copy = new \Google_Service_Drive_DriveFile();
    $copy->setTitle($documentName);

    try {
        $createdFile = $service->files->copy($source_id, $copy);
    } catch (\Exception $e) {
        print "An error occurred: " . $e->getMessage();
    }

And the output:

An error occurred: Error calling POST https://www.googleapis.com/drive/v2/files/1RXglHS8P4Klcn28WTsf-QHj9BuBIV0AY3R6PeJitqcM/copy: (404) File not found: 1PKaE72BI0ux3uAv3vCKv4pAT_jsxhJ5uZOY0F1sEcNw

It seems like there's an issue with permissions on the destination file? How can I verify if there's a permission or authentication issue? I'm logged in with OAuth just fine.

Here is the client connection code:

static public function connect()
{
    self::$shared_folder = config('google.SharedFolder');

    $client_email = config('google.ServiceAccountEmail');

    // See if we need to recreate the client
    if (!GoogleDrive::$client) {

        $private_key = file_get_contents(config('google.ServiceAccountKeyFile'));

        GoogleDrive::$client = new \Google_Client();

        GoogleDrive::$client->setApplicationName(GoogleDrive::$app_name);
        $credentials = new \Google_Auth_AssertionCredentials(
            $client_email,
            GoogleDrive::$scope,
            $private_key
        );
        GoogleDrive::$client->setAssertionCredentials($credentials);
    }

    // Always see if the token is expired
    if (GoogleDrive::$client->getAuth()->isAccessTokenExpired()) {
        GoogleDrive::$client->getAuth()->refreshTokenWithAssertion();
    }

    $_SESSION['service_token'] = GoogleDrive::$client->getAccessToken();

    return new \Google_Service_Drive(GoogleDrive::$client);
}

1 个答案:

答案 0 :(得分:0)

不是一个完整的答案,但我们最终在域中使用常规帐户并将其令牌存储到复制文件中。我们无法使用服务帐户驱动器来存储和复制文件。我们也无法授予用户对服务帐户驱动器中文件的权限。我们甚至尝试使用服务帐户作为所有者在普通用户的驱动器中创建文档,但副本和权限失败。我们能够以服务帐户作为所有者正确创建文件,但显然我们要么丢失了某些内容,要么做了一些不允许的事情。