duanjiaonie6097 2016-04-21 22:16
浏览 105

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条回答 默认 最新

  • dtch60248 2016-04-29 15:53
    关注

    Not a complete answer, but we ended up using a regular account in the domain and storing their tokens to copy files. We were not able to use the Service Account Drive to store and copy files. Nor were we able to grant users permissions to files in the Service Account Drive. We even tried creating documents in a regular user's Drive with the Service Account as the owner but copies and permissions failed. We were able to create files properly with the Service Account as the owner, but apparently we are either missing something or doing something not permissible.

    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源