googleapis / google-api-php-client

A PHP client library for accessing Google APIs
http://googleapis.github.io/google-api-php-client/
Apache License 2.0
9.32k stars 3.52k forks source link

error 400 : "Media type 'application/json; charset=UTF-8' is not supported." #2436

Closed afsh7n closed 1 year ago

afsh7n commented 1 year ago

Hello, have a good time, I use Google api v3 to upload videos to YouTube. Unfortunately, I am facing this error since yesterday without any changes in the code or the server.

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

Thank you

Environment details

"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);
bshaffer commented 1 year ago

Hello. I see you've also asked this question on Stack Overflow.

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

So I would try setting $resumable = true (which is the fifth argument to MediaFileUpload). You can see an example of this in large-file-upload.php.