Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google api 如何通过驱动API(PHP客户端)创建公共Google文档_Google Api_Google Drive Api_Google Api Client_Google Api Php Client - Fatal编程技术网

Google api 如何通过驱动API(PHP客户端)创建公共Google文档

Google api 如何通过驱动API(PHP客户端)创建公共Google文档,google-api,google-drive-api,google-api-client,google-api-php-client,Google Api,Google Drive Api,Google Api Client,Google Api Php Client,这就是我到目前为止所得到的,通过结合和: 它可以工作,这意味着它创建了一个文本/普通文件,并返回一个包含元数据的数组。然而,我想要的是创建一个Google文档,而不是文本/纯文本。当然,我尝试将mime类型(两种外观)更改为“application/vnd.google apps.document”,但得到了以下结果: 致命错误:在/local/path/to/Google-api-php-client/src/io/Google_-REST.php:66中出现未捕获的异常“Google_-Se

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

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

致命错误:在/local/path/to/Google-api-php-client/src/io/Google_-REST.php:66中出现未捕获的异常“Google_-ServiceException”,并显示消息“error calling POST:(400)Bad Request”

此外,我需要公开访问该文档。当我通过上述方法上传一个文本/普通文件,然后尝试浏览到它(从不同于上传者的帐户),我被告知我需要权限。我仔细查看了
$createdFile
中的数组,发现一个没有值的“共享”键,所以我很天真地尝试使用以下命令将其设置为1:

$file->setShared( 1 );

它不起作用,而且,当我再次查看数组时,我注意到“shared”键仍然没有赋值给它。我在网上查找任何可能对我有帮助的文档,但运气不佳。有人能帮我吗?谢谢

经过大量阅读和测试,我找到了问题的答案

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

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

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

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

我已经在Moodle LMS系统中实现了这个插件,下面是源代码,其中包括一个PHP类,用于实现对文档的创建和设置适当的权限。

实际相关类别如下所示:

嘿,伙计们,我也在试着这么做,但我找不到新的Google_权限()的代码。我的错误总是-致命错误:在中找不到类“Google_权限”。。。有人能给我一个来源的链接吗没有这个班!!!是的,和@SuperYegorius一样的问题。我找不到图坦卡蒙使用的Google_权限。看起来它在没有更新文档或phpdoc的情况下被重命名为
Google_服务_驱动器_权限
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 );