谷歌驱动PHP API,插入文件驱动器

时间:2014-11-13 22:32:04

标签: php google-drive-api

所以基本上谷歌开发者的文档没有更新,以匹配他们的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

如果您有任何想法我是否应该进口其他课程或其他什么,请告知。谢谢。

3 个答案:

答案 0 :(得分:1)

我对同样缺乏新文档感到苦苦挣扎,在我全力以赴之后,我做了一个简单的例子,说明如何谷歌oAuth,驱动上传和在Google Drive SDK中创建文件夹以帮助其他人。你可以在GitHub上分叉它:https://github.com/numsu/google-drive-sdk-api-php-insert-file-parent-example

祝你好运!

答案 1 :(得分:0)

我认为你没有一个名为$ files的对象。创建符合您要执行的操作的$ files的新实例,然后执行插入。希望有所帮助。

答案 2 :(得分:0)

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

$createdFile = $service->files->create( $file, array (
    'data' => $data,
    'mimeType' => 'text/plain',
    'uploadType' => 'media'
) );
相关问题