簡體   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