2014-11-13 50 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' 
)); 
相关问题