我制作了一个简单的PHP脚本,可以将文件上传到Google云端硬盘。然后我运行以下函数:
function PublishToWeb($service, $fileId, $revisionId) {
$patchedRevision = new Google_Revision();
$patchedRevision->setPublished(true);
$patchedRevision->setPublishAuto(true);
$patchedRevision->setPublishedOutsideDomain(true);
try {
return $service->revisions->patch($fileId, $revisionId, $patchedRevision);
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
return NULL;
}
我没有收到任何错误消息,但未发布word文档。
当我尝试使用Google API资源管理器设置标记时,它不会返回任何错误,但也无法将已发布的标记设置为true。我错过了一些明显的东西吗?
为清楚起见,我正在尝试上传文件,然后立即模拟按“发布到网络”。我也尝试过使用revisions.update
更新
好的,我已经发现该文档必须上传并转换为谷歌doc格式才能发布。但是,当文档保存为google doc时,它没有设置headrevisionid,因此我无法使用revisions.update或revisions.patch
任何人都知道如何发布Google doc文件?
答案 0 :(得分:2)
好的,我明白了。
将文件上传时转换为google doc
$createdFile = $service->files->insert($file, array(
'data' => $data,
'convert' => 'true',
));
然后将已发布的标志更新为true
$revisionId = 1; //The revisionId for a newly created google doc will = 1
function updateRevision($service, $fileId, $revisionId) {
$patchedRevision = new Google_Revision();
$patchedRevision->setPublished(true);
$patchedRevision->setPublishAuto(true);
$patchedRevision->setPublishedOutsideDomain(true);
try {
return $service->revisions->patch($fileId, $revisionId, $patchedRevision);
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
return NULL;
}
然后构建已发布的链接,因为这不是为您完成的
$PublishURL = 'https://docs.google.com/document/d/'.$fileId.'/pub';