2014-11-13 53 views
0

因此,基本上google開發人員的文檔不會更新以匹配其新版本的api。Google Drive PHP api,將文件插入驅動器

這裏是我的代碼:

<?php 
require_once 'autoload.php'; 

$client = new Google_Client(); 
// Get your credentials from the console 
$client->setClientId ('CLIENT_ID'); 
$client->setClientSecret ('CLIENT_SECRET'); 
$client->setRedirectUri ('urn:ietf:wg:oauth:2.0:oob'); 
$client->setScopes (array (
     'https://www.googleapis.com/auth/drive' 
)); 

$service = new Google_Service ($client); 

$authUrl = $client->createAuthUrl(); 

// Request authorization 
print "Please visit:\n$authUrl\n\n"; 
print "Please enter the auth code:\n"; 
$authCode = trim (fgets (STDIN)); 

// Exchange authorization code for access token 
$accessToken = $client->authenticate ($authCode); 
$client->setAccessToken ($accessToken); 

// Insert a file 
$file = new Google_Service_Drive_DriveFile(); 
$file->setTitle ('My document'); 
$file->setDescription ('A test document'); 
$file->setMimeType ('text/plain'); 

$data = file_get_contents ('document.txt'); 

$createdFile = $service->files->insert ($file, array (
     'data' => $data, 
     'mimeType' => 'text/plain' 
)); 

echo $createdFile; 
?> 

這是錯誤我得到:

PHP Notice: Undefined property: Google_Service::$files in D:\DATA\java\php workspace\Google Drive api test\quickstart.php on line 34 
PHP Fatal error: Call to a member function insert() on a non-object in D:\DATA\java\php workspace\Google Drive api test\quickstart.php on line 34 

這是有問題的代碼行:

$createdFile = $service->files->insert 

如果您有任何想法,如果我應該進口一些其他類或東西,請告知。謝謝。

回答

0

我想你沒有一個名爲$ files的對象,我可以告訴。創建符合您正在嘗試執行的操作的$ files文件的新實例,然後執行插入操作。希望有所幫助。

0

如果是v3,則需要使用create。插入在v3中不可用

$createdFile = $service->files->create($file, array (
    'data' => $data, 
    'mimeType' => 'text/plain', 
    'uploadType' => 'media' 
)); 
相關問題