(400) 无效请求.上传的字节数必须等于或大于 262144,但最终请求除外



一旦视频通过数据 API V3 上传,我正在尝试通过 API 设置自定义缩略图。

我收到以下错误:

Error calling PUT https://www.googleapis.com/upload/youtube/v3/thumbnails/set?videoId=[VIDEOID]&key=[KEY]&uploadType=resumable&upload_id=[UPLOADID]: (400) Invalid request.  The number of bytes uploaded is required to be equal or greater than 262144, except for the final request (it's recommended to be the exact multiple of 262144).  The received request contained 8192 bytes, which does not meet this requirement.

上传的缩略图满足所有要求,例如* 宽高比为 16:9*尺寸为1280 * 720* 图像类型为 png

                $thumbnailFile = $thumbnailFile;
                $client = new Google_Client();
                $youtube = new Google_Service_YouTube($client);
                $client->setAccessToken($accessToken);
                $io = new Google_IO_Stream($client);
                $io->setTimeout(180);
                if ($client->getAccessToken()) {
                    $imagePath = $thumbnailFile;
                    $headers = get_headers($imagePath, 1);
                    $file_size= $headers["Content-Length"];
                    $mime_type = null;
                    if($headers['Content-Type'] == 'image/png' || $headers['Content-Type'] == 'image/jpeg'){
                        $mime_type = $headers['Content-Type'];
                    }

                    $chunkSizeBytes = 1 * 1024 * 100;
                    $client->setDefer(true);
                    $params = array('videoId' => $videoId);
                    $setRequest = $youtube->thumbnails->set($videoId);
                    $media = new Google_Http_MediaFileUpload(
                        $client,
                        $setRequest,
                        $mime_type,
                        $file_size,
                        true,
                        false
                    );
                    $media->setFileSize($file_size);

                    $status = false;
                    $handle = fopen($imagePath, "rb");
                    while (!$status && !feof($handle)) {
                        $chunk = fread($handle, $chunkSizeBytes);
                        $status = $media->nextChunk($chunk);
                    }
                    fclose($handle);
                    $client->setDefer(false);
                    $status['code'] = 1;
                    $status['message'] = 'Thumbnail uploaded successfully';

setFileSize期望传递您正在上传的实际文件的大小

在您的示例中,您设置了$file_size= $headers["Content-Length"];,然后$media->setFileSize($file_size);

请尝试更改为$media->setFileSize(filesize($imagePath));$file_size = filesize($imagePath);

最新更新