繁体   English   中英

Google Drive API:使用服务帐户复制文件

[英]Google Drive API: copy file with service account

编辑:复制通过API创建的服务帐户的文件时,副本会通过,但是此错误仍然存​​在,并且错误中的文件ID与副本相对应。 复制文件时,服务帐户不拥有(但具有编辑权限),不会进行复制,并且错误中的文件ID是针对原始文件的。

该服务帐户似乎在通过API创建的文件之外没有可见性,并且对这些文件的副本也没有可见性。

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

我收到与此问题相同的错误: Google Drive API copy()-404错误,除了我的设置有所不同。

创建文件工作正常。

我正在尝试通过副本创建文档,该文档将不归用户所有,但仍为他们提供读/写/无下载权限。 如果现在就复制,我会很高兴。

这是代码:

    $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();
    }

并输出:

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

似乎目标文件的权限存在问题? 如何验证是否存在权限或身份验证问题? 我已经使用OAuth登录。

这是客户端连接代码:

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);
}

这不是一个完整的答案,但是我们最终在域中使用了常规帐户并存储了其令牌以复制文件。 我们无法使用服务帐户驱动器来存储和复制文件。 我们也无法授予用户访问服务帐户驱动器中文件的权限。 我们甚至尝试使用服务帐户作为所有者在普通用户的云端硬盘中创建文档,但是复制和权限失败。 我们可以使用服务帐户作为所有者来正确创建文件,但是显然我们丢失了某些东西或做了不允许的事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM