谷歌驱动器PHPapi,插入文件到驱动器


Google Drive PHP api, insert file to drive

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

如果您知道我是否应该导入其他类或其他东西,请提供建议。谢谢。

我一直在为同样缺乏新文档而苦苦挣扎,在我弄清楚这一切之后,我做了一个简单的例子,说明如何在谷歌云端硬盘 SDK 中创建谷歌 oAuth、驱动器上传和文件夹创建来帮助他人。你可以在GitHub上分叉:https://github.com/numsu/google-drive-sdk-api-php-insert-file-parent-example

祝你好运!

我想你没有我能说出的名为$files的对象。创建一个符合您尝试执行的操作的新$files实例,然后执行插入。希望有帮助。

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

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