2
votes

I have made a simple PHP script which uploads a file to Google Drive. I then run the following function:

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

I get no error message but the word document is not published.

When I try to set the flags using the Google APIs explorer it returns no errors but also fails to set the published flag to true. Am I missing something obvious?

For clarity I'm trying to upload a file then instantly simulate pressing 'Publish to web'. I also tried using revisions.update

Update:

Okay, I've figured out that the document has to be uploaded and converted to a google doc format to be published. However when the document is saved as a google doc it has no headrevisionid set so I can't use revisions.update or revisions.patch

Anyone know how to publish a google doc file?

1

1 Answers

3
votes

Okay I figured it out.

When uploading the file convert to a google doc

$createdFile = $service->files->insert($file, array(
  'data' => $data,
  'convert' => 'true',
));

Then update the published flag to 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;
    }

Then build the publishedlink as this isn't done for you

$PublishURL = 'https://docs.google.com/document/d/'.$fileId.'/pub';