編輯: 複製服務帳戶通過API創建的文件時,副本會經過,但此錯誤仍然存在,並且錯誤中的文件ID與副本相對應。當複製文件服務帳戶不擁有(但具有編輯訪問權限)時,副本不會發生,並且錯誤中的文件ID是原始文件。Google Drive API:複製帶有服務帳戶的文件
該服務帳戶在通過API創建的文件之外似乎沒有可見性,並且不具有這些文件副本的可見性。
= - = - = - = - = - = - = - = - = - = - = - = - = -
我收到了同樣的錯誤,因爲這問題:Google Drive API copy() - 404 error,除了我的設置是一個有點不同。
創建文件工作正常。
我正在嘗試通過副本創建文檔,該副本不會被用戶所有,但仍然會給他們讀/寫/無下載訪問權限。如果現在只是複製,我會很高興。
下面是代碼:
$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);
}