简体   繁体   English

Google Drive PHP API,将文件插入驱动器

[英]Google Drive PHP api, insert file to drive

So basically the documentation in google developer is not updated to match the new version of their api. 因此,基本上Google开发人员中的文档不会更新为与其api的新版本匹配。

Here is my code: 这是我的代码:

<?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;
?>

And this is the error I get: 这是我得到的错误:

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

This is the problematic line of code: 这是有问题的代码行:

$createdFile = $service->files->insert

If you have any idea if I should be importing some other class or something please advise. 如果您有任何想法要不要导入其他类或其他内容,请告知。 Thank you. 谢谢。

I was struggling with the same lack of new documentation, after I figured it all out, I made a simple example of how-to google oAuth, drive upload and folder creating in Google Drive SDK to help others. 在弄清所有新文档后,我一直都在苦苦挣扎,我做了一个简单的示例,介绍了如何使用Google oAuth,如何在Google Drive SDK中创建驱动器上传和创建文件夹来帮助其他人。 You can fork it on GitHub: https://github.com/numsu/google-drive-sdk-api-php-insert-file-parent-example 您可以在GitHub上对其进行分叉: https//github.com/numsu/google-drive-sdk-api-php-insert-file-parent-example

Good luck! 祝好运!

I think you do not have an object named $files that I can tell. 我认为您没有可以告诉我的名为$ files的对象。 Create a new instance of $files that conforms to what you are trying to do and then perform the insert. 创建一个符合您要执行的操作的$ files新实例,然后执行插入操作。 Hopes that help. 希望对您有所帮助。

If it's v3 you need to use create. 如果是v3,则需要使用create。 Insert is not available in v3 插入在v3中不可用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM