如何通过Drive API(PHP客户端)创建公共Google文档 [英] How to create a public Google Doc through the Drive API (PHP client)

查看:150
本文介绍了如何通过Drive API(PHP客户端)创建公共Google文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,这是通过结合这个

  require_oncegoogle-api-php-client / src / Google_Client.php; 
require_oncegoogle-api-php-client / src / contrib / Google_DriveService.php;
require_oncegoogle-api-php-client / src / contrib / Google_Oauth2Service.php;

//首先,建立授权的服务一个Drive服务对象占
$ AUTH =新Google_AssertionCredentials(
DRIVE_SERVICE_ACCOUNT_EMAIL,
阵列(DRIVE_SCOPE)
file_get_contents(DRIVE_SERVICE_ACCOUNT_KEY)
);
$ client = new Google_Client();
$ client-> setUseObjects(true);
$ client-> setAssertionCredentials($ auth);
$ service = new Google_DriveService($ client);

//然后,插入文件
$ file = new Google_DriveFile();
$ file-> setTitle('我的文档');
$ file-> setMimeType('text / plain');
$ createdFile = $ service-> files-> insert($ file,array(
'data'=>'Hello world!',
'mimeType'=>'文本/纯文本',
));

print_r($ createdFile);

它可以工作,这意味着它会创建一个文本/纯文件并返回一个包含其元数据的数组。但是,我想要的是创建一个Google文档,而不是文本/纯文本。我尝试了将mime类型(两种外观)改为application / vnd.google-apps.document,但得到了以下结果:

致命错误:未捕获的异常'Google_ServiceException'消息'Error calling POST https://www.googleapis。 com / upload / drive / v2 / files?uploadType = multipart :(400)Bad Request'in /local/path/to/google-api-php-client/src/io/Google_REST.php:66



另外,我需要公开文档。当我通过上述方法上传文本/纯文件,然后尝试浏览它时(来自不同于上传者的帐户),我被告知需要许可。我仔细研究了 $ createdFile 中的数组,并注意到一个没有值的共享键,所以我很天真地尝试将它设置为1:

  $ file-> setShared(1); 

它不起作用,而且当我再次查看数组时,我注意到'共享'键仍然没有分配给它的值。我在网上查找任何可能帮助我的文档,但没有运气。任何人都可以帮助我?谢谢!

解决方案

经过多次阅读和测试,我找到了我的问题的答案。

问题在于文件的数据参数:Google文档不能将纯文本视为数据(其数据需要包含某些标头或其他内容)。因此,通过删除'data'参数(以及'mimeType',为什么不),我得到的错误消失了。

关于权限,解决方案是首先使用默认权限创建文件,然后添加一个新文件。



下面我粘贴最小代码来创建可公开访问的Google文档:

  require_oncegoogle-api-php-client / src / Google_Client.php; 
require_oncegoogle-api-php-client / src / contrib / Google_DriveService.php;
require_oncegoogle-api-php-client / src / contrib / Google_Oauth2Service.php;

//建立授权与服务帐号
$权威性=新Google_AssertionCredentials(
DRIVE_SERVICE_ACCOUNT_EMAIL,
阵列(DRIVE_SCOPE),
的file_get_contents驱动器服务对象(DRIVE_SERVICE_ACCOUNT_KEY)
);
$ client = new Google_Client();
$ client-> setUseObjects(true);
$ client-> setAssertionCredentials($ auth);
$ service = new Google_DriveService($ client);

//创建文件
$ file = new Google_DriveFile();
$ file-> setTitle('Hello world!');
$ file-> setMimeType('application / vnd.google-apps.document');
$ file = $ service-> files-> insert($ file);

//授予每个人读取和写入文件的权限
$ permission = new Google_Permission();
$ permission-> setRole('writer');
$ permission-> setType('anyone');
$ permission-> setValue('me');
$ service-> permissions-> insert($ file-> getId(),$ permission);

print_r($ file);


This is what I've got so far, by combining this and this:

require_once "google-api-php-client/src/Google_Client.php";
require_once "google-api-php-client/src/contrib/Google_DriveService.php";
require_once "google-api-php-client/src/contrib/Google_Oauth2Service.php";

//First, build a Drive service object authorized with the service accounts
$auth = new Google_AssertionCredentials(
    DRIVE_SERVICE_ACCOUNT_EMAIL,
    array( DRIVE_SCOPE ),
    file_get_contents( DRIVE_SERVICE_ACCOUNT_KEY )
);
$client = new Google_Client();
$client->setUseObjects( true );
$client->setAssertionCredentials( $auth );
$service = new Google_DriveService( $client );

//Then, insert the file
$file = new Google_DriveFile();
$file->setTitle( 'My document' );
$file->setMimeType( 'text/plain' );
$createdFile = $service->files->insert( $file, array(
    'data' => 'Hello world!',
    'mimeType' => 'text/plain',
));

print_r( $createdFile );

It works, meaning that it creates a text/plain file and returns me an array with its metadata. However, what I want is to create a Google Document, not a text/plain. I tried of course changing the mime type (both appearances) to "application/vnd.google-apps.document", but got the following:

Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling POST https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart: (400) Bad Request' in /local/path/to/google-api-php-client/src/io/Google_REST.php:66

Also, I need the document to be publicly accessible. When I upload a text/plain file through the above method, and then try to browse to it (from an account different of the uploader's), I'm told that I need permission. I took a closer look at the array in $createdFile and noticed a 'shared' key with no value, so quite naively I tried setting it to 1 with:

$file->setShared( 1 );

It didn't work, and also, when I looked again at the array, I noticed that the 'shared' key had still no value assigned to it. I looked online for any documentation that may help me, but no luck. Anyone can help me out? Thanks!!

解决方案

After much reading and testing, I found the answer to my question.

The problem was the 'data' parameter of the file: the Google Docs cannot have plain text as data (their data needs to include some headers or something). So by deleting the 'data' parameter (and the 'mimeType' too, why not), the error I got disappeared.

As to the permissions, the solution was to first create the file with the default permissions, and then add a new one.

Below I paste the minimal code to create a publicly accessible Google Doc:

require_once "google-api-php-client/src/Google_Client.php";
require_once "google-api-php-client/src/contrib/Google_DriveService.php";
require_once "google-api-php-client/src/contrib/Google_Oauth2Service.php";

//Build the Drive Service object authorized with your Service Account
$auth = new Google_AssertionCredentials(
    DRIVE_SERVICE_ACCOUNT_EMAIL,
    array( DRIVE_SCOPE ),
    file_get_contents( DRIVE_SERVICE_ACCOUNT_KEY )
);
$client = new Google_Client();
$client->setUseObjects( true );
$client->setAssertionCredentials( $auth );
$service = new Google_DriveService( $client );

//Create the file
$file = new Google_DriveFile();
$file->setTitle( 'Hello world!' );
$file->setMimeType( 'application/vnd.google-apps.document' );
$file = $service->files->insert( $file );

//Give everyone permission to read and write the file
$permission = new Google_Permission();
$permission->setRole( 'writer' );
$permission->setType( 'anyone' );
$permission->setValue( 'me' );
$service->permissions->insert( $file->getId(), $permission );

print_r( $file );

这篇关于如何通过Drive API(PHP客户端)创建公共Google文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆