2015-01-06 35 views
0

我試圖上傳文件到驅動器使用服務帳戶。我收到以下錯誤,說明權限不足。谷歌驅動器的多部分上傳返回 - (403)權限不足

錯誤調用POST https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart:這裏(403)沒有足夠的權限」

是我的總的代碼。

$client_id = '69649809374-ib5c5qmr7n34adsfsdfsxxxifss95ue7qouplh7v3r.apps.googleusercontent.com'; //Client ID 
$service_account_name = '[email protected]account.com'; //Email Address 
$key_file_location = 'secretkey.p12'; //key.p12 

echo pageHeader("Service Account Access"); 
if ($client_id == '269649809374-b5c5qmr7n34aifss95ue7qouplh7v3r.apps.googleusercontent.com' 
    || !strlen($service_account_name) 
    || !strlen($key_file_location)) { 
     echo missingServiceAccountDetailsWarning(); 
     } 

$client = new Google_Client(); 
$client->setApplicationName("Client_Library_Examples"); 

if (isset($_SESSION['service_token'])) 
    { 
    $client->setAccessToken($_SESSION['service_token']); 
    } 
$key = file_get_contents($key_file_location); 

$cred = new Google_Auth_AssertionCredentials(
    $service_account_name, 
    array('https://www.googleapis.com/auth/drive'), 
    $key); 

$client->setAssertionCredentials($cred); 
$client->addScope("https://www.googleapis.com/auth/drive"); 

if ($client->getAuth()->isAccessTokenExpired()) { 
    $client->getAuth()->refreshTokenWithAssertion($cred); 
    } 
$_SESSION['service_token'] = $client->getAccessToken(); 


$service = new Google_Service_Drive($client); 

$file = new Google_Service_Drive_DriveFile(); 
$parentId='0B7yersFxlKMTR2RsMThQSG1RUVk'; 
$parent = new Google_Service_Drive_ParentReference(); 

// create a file 

DEFINE("TESTFILE", 'testfile-small.txt'); 
if (!file_exists(TESTFILE)) 
         { 
        $fh = fopen(TESTFILE, 'w'); 
         fseek($fh, 1024 * 1024); 
        fwrite($fh, "this is sekhar", 1); 
         fclose($fh); 
         } 

        $parent->setId($parentId); 
        $file->setParents(array($parent)); 
         $file->setTitle("I am service Account"); 

        $result = $service->files->insert($file,array('data' => file_get_contents(TESTFILE), 
     'mimeType' => 'application/octet-stream', 
     'uploadType' => 'media')); 

有沒有我的代碼有問題。

回答

2
403) Insufficient Permission' 

意味着您的服務帳戶無權訪問您要求的操作。

我會檢查的第一件事就是您已正確授予服務帳戶訪問有問題的目錄。它是一個在你的驅動器上,還是服務帳戶所擁有的一個? (你給一個服務帳戶訪問一個驅動器文件夾,就像普通用戶使用服務帳戶的電子郵件地址並添加它一樣,它將需要寫入訪問權限。)

由於您沒有執行目錄列表,因此您可能還要仔細檢查您是否擁有正確的目錄ID。或者使用root來測試你的上傳代碼。

'0B7yersFxlKMTR2RsMThQSG1RUVk' 
+0

感謝DalmTo,與文件夾的問題,現在我能夠能上傳文件,但我怎麼能訪問它們,當我試圖訪問他們,說其沒有權限,我在哪裏可以更改權限查看它通過公開,以便我可以將它們嵌入我的應用程序 –

+0

這是我認爲的另一個問題。 – DaImTo