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

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

问题描述

这是我到目前为止所得到的,通过结合 this这个:

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

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

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:

致命错误:未捕获的异常Google_ServiceException",消息为调用 POST 时出错https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart:/local/path/to/google-api-php-client/src/中的 (400) 错误请求io/Google_REST.php:66

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

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

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.

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

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.

下面我粘贴了用于创建可公开访问的 Google 文档的最少代码:

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 Doc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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