Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create a public Google Doc through the Drive API (PHP client)
    primarykey
    data
    text
    <p>This is what I've got so far, by combining <a href="https://developers.google.com/drive/service-accounts#google_apis_console_project_service_accounts" rel="nofollow">this</a> and <a href="https://developers.google.com/drive/quickstart-php" rel="nofollow">this</a>:</p> <pre><code>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-&gt;setUseObjects( true ); $client-&gt;setAssertionCredentials( $auth ); $service = new Google_DriveService( $client ); //Then, insert the file $file = new Google_DriveFile(); $file-&gt;setTitle( 'My document' ); $file-&gt;setMimeType( 'text/plain' ); $createdFile = $service-&gt;files-&gt;insert( $file, array( 'data' =&gt; 'Hello world!', 'mimeType' =&gt; 'text/plain', )); print_r( $createdFile ); </code></pre> <p>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:</p> <p>Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling POST <a href="https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart" rel="nofollow">https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart</a>: (400) Bad Request' in /local/path/to/google-api-php-client/src/io/Google_REST.php:66</p> <p>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 <code>$createdFile</code> and noticed a 'shared' key with no value, so quite naively I tried setting it to 1 with:</p> <pre><code>$file-&gt;setShared( 1 ); </code></pre> <p>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!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload