Error 400: "Media type 'application/json; charset=UTF-8' is not supported."
P粉818561682
P粉818561682 2024-03-28 18:05:10
0
1
421

Have fun, I use Google api v3 to upload videos to YouTube. Unfortunately I'm facing this error since yesterday without any changes in the code or server.

I searched for the error text in different sections but didn't get any results. The only thing I found was this package and the file at this address src/Http/MediaFileUpload.php Specify content-type, the value is application/json; set charset=UTF-8. I think that's the problem. Please guide me.

Thanks

Environment details

  • Operating system: Linux
  • PHP version: 8.1.8
  • Package name and version: "google/apiclient": "^2.13.2"
  • Laravel:^9.0

Steps to reproduce

"error": {
    "code": 400,
    "message": "Media type 'application/json; charset=UTF-8' is not supported. ",
    "errors": [
      {
        "message": "Media type 'application/json; charset=UTF-8' is not supported. ",
        "domain": "global",
        "reason": "badRequest"
      }
    ],
    "status": "INVALID_ARGUMENT"
  }

Code Example

 $chunkSizeBytes = 15 * 1024 * 1024;
        $client->setDefer(true);
        $insertRequest = $youtube->videos->insert("status,snippet,recordingDetails", $video , [
            'notifySubscribers' => true
        ]);
        $media = new MediaFileUpload(
            $client,
            $insertRequest,
            'video/'.File::extension($videoPath),
            null,
            false,
            $chunkSizeBytes
        );
        $media->setFileSize(File::size($videoPath));

        $status = false;
        $handle = fopen($videoPath, "rb");
        while (!$status && !feof($handle)) {
            $chunk = fread($handle, $chunkSizeBytes);
            $status = $media->nextChunk($chunk);
        }
        fclose($handle);

        $client->setDefer(false);

P粉818561682
P粉818561682

reply all(1)
P粉872101673

Looking at the code, the media type application/json occurs when $resumable is false (see here and here).

So I would try setting $resumable = true (which is the fifth parameter of MediaFileUpload). You can find it at large-file-upload.php.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template