2017-10-17 170 views
0

我知道,有數百個職位涉及這一話題的變化,我已經花了幾個小時,通過他們讀,但我不能得到這個我的谷歌雲端硬盤帳戶工作。上傳文件使用PHP

我想從文件通過谷歌驅動器SDK(我使用V2.2.0)使用PHP我的服務器上傳到我的谷歌雲端硬盤帳戶。

該腳本執行得很好,它似乎在某處上傳,甚至返回一個文件ID(始終是同一個),但它沒有顯示在我的Google雲端硬盤中,並且在嘗試查看文件時收到以下消息:

{ 
"error": { 
    "errors": [ 
    { 
    "domain": "global", 
    "reason": "notFound", 
    "message": "File not found: <FILE_ID>.", 
    "locationType": "parameter", 
    "location": "fileId" 
    } 
    ], 
    "code": 404, 
    "message": "File not found: <FILE_ID>." 
} 
} 

我已經設置了服務帳戶並下載了JSON憑證。我的目標是創建一個上傳存儲在我的服務器上的文件的功能,所以我不需要oAuth同意屏幕。

這裏的測試代碼至今:

<?php 

    include_once __DIR__ . '/vendor/autoload.php'; 

    putenv('GOOGLE_APPLICATION_CREDENTIALS=../../../keys/MyProject-xxxxxxxxxxxx.json'); 

    $client = new Google_Client(); 
    $client->addScope(Google_Service_Drive::DRIVE); 
    $client->useApplicationDefaultCredentials(); 
    $client->setApplicationName("MyApplication"); 
    $client->setScopes(['https://www.googleapis.com/auth/drive.file']); 

    $file = new Google_Service_Drive_DriveFile(); 
    $file->setName(uniqid().'.jpg'); 
    $file->setDescription('A test document'); 
    $file->setMimeType('image/jpeg'); 

    $data = file_get_contents('cat.jpg'); 

    $service = new Google_Service_Drive($client); 

    $createdFile = $service->files->create($file, array(
    'data' => $data, 
    'mimeType' => 'image/jpeg', 
    'uploadType' => 'multipart')); 

    echo("<a href='https://www.googleapis.com/drive/v3/files/".$createdFile->id."?key=<MY_API_KEY>&alt=media' target='_blank'>Download</a>"); 

回答

1

解決了!

您需要創建谷歌雲端硬盤中的文件夾,然後將服務帳戶的電子郵件地址添加到名單的人誰可以添加&編輯。

然後,你需要在你的代碼到指定的文件夾ID:

$file->setParents(array("0B_xxxxxxxxxxxxxxxxxxxxxxxxx"));