Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>After much reading and testing, I found the answer to my question.</p> <p>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.</p> <p>As to the permissions, the solution was to first create the file with the default permissions, and then add a new one.</p> <p>Below I paste the minimal code to create a publicly accessible Google Doc:</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"; //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-&gt;setUseObjects( true ); $client-&gt;setAssertionCredentials( $auth ); $service = new Google_DriveService( $client ); //Create the file $file = new Google_DriveFile(); $file-&gt;setTitle( 'Hello world!' ); $file-&gt;setMimeType( 'application/vnd.google-apps.document' ); $file = $service-&gt;files-&gt;insert( $file ); //Give everyone permission to read and write the file $permission = new Google_Permission(); $permission-&gt;setRole( 'writer' ); $permission-&gt;setType( 'anyone' ); $permission-&gt;setValue( 'me' ); $service-&gt;permissions-&gt;insert( $file-&gt;getId(), $permission ); print_r( $file ); </code></pre>
 

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