Youtube api (400)无效请求。上传的字节数要求等于或大于262144,但最终请求除外

Youtube api (400)无效请求。上传的字节数要求等于或大于262144,但最终请求除外,youtube-api,youtube-data-api,Youtube Api,Youtube Data Api,我试图通过API设置自定义缩略图,只要视频通过DataAPIv3上传 我得到以下错误: 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 u

我试图通过API设置自定义缩略图,只要视频通过DataAPIv3上传

我得到以下错误:

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';

该错误似乎表明您的文件只有8.192KB,并且应该至少为262.144KB

setFileSize
希望传递给您正在上载的实际文件的大小

在您的示例中,设置了
$file_size=$headers[“Content Length”]
然后
$media->setFileSize($file\u size)


请尝试更改为
$media->setFileSize(filesize($imagePath))
$file\u size=filesize($imagePath)

谢谢你的回答,我试图上传的图像大约是371KB