简体   繁体   中英

How to create a new Google Docs file with Google Drive PHP API?

I am trying to create a new Google document in Google Drive by using the API. According to the documentation the mimeType is valid but I am getting a response back that it is not. This works if I specify other mime types. What am I doing wrong?

    $this->driveService->files->create(
        new Drive\DriveFile(
            array(
                'name' => 'testt',
                'parents' => [ self::DRIVE_FOLDER ]
            )
        ),
        array (
            'data' => 'test',
            'mimeType' => 'application/vnd.google-apps.document',
            'supportsAllDrives' => true
        )
    );

The error I get is:

Uncaught Google\Service\Exception: {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "invalidContentType",
    "message": "Invalid MIME type provided for the uploaded content.",
    "locationType": "other",
    "location": "media.mimeType"
   }
  ],
  "code": 400,
  "message": "Invalid MIME type provided for the uploaded content."
 }
}

In your script, how about the following modification?

Modified script:

$this->driveService->files->create(
    new Drive\DriveFile(
        array(
            'name' => 'testt',
            'parents' => [ self::DRIVE_FOLDER ],
            'mimeType' => 'application/vnd.google-apps.document',
        )
    ),
    array (
        'data' => 'test',
        'mimeType' => 'text/plain',
        'supportsAllDrives' => true,
    )
);
  • I thought that the mimeType of application/vnd.google-apps.document is required to be included in the file metadata. And, the upload data is text data. So, text/plain is added as the mimeType of data.

  • When this script is run, a new Google Document including a text of test is created to the specific folder on Google Drive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM