googleapis / google-api-php-client

A PHP client library for accessing Google APIs
Apache License 2.0
9.22k stars 3.52k forks source link

Insert video youtube error #2469

Closed padi-dev-lucvt closed 11 months ago

padi-dev-lucvt commented 1 year ago

Environment details

Code example

    // set the video's title, description
    $snippet = new \Google_Service_YouTube_VideoSnippet();
    $snippet->setTitle('Upload video using API');
    $snippet->setDescription('Video description');

    // Set the video's status to "private". 
    $status = new \Google_Service_YouTube_VideoStatus();
    $status->privacyStatus = 'private';

    // Associate the snippet and status objects with a new video resource.
    $video = new \Google_Service_YouTube_Video();
    $video->setSnippet($snippet);
    $video->setStatus($status);

    // Specify the size of each chunk of data
    $chunkSizeBytes = 1 * 1024 * 1024;

    // Setting the defer flag to true tells the client to return a request which can be called
    // with ->execute(); instead of making the API call immediately.
    $this->client->setDefer(true);

    // Create a request for the API's videos.insert method
    $insertRequest = $service->videos->insert("status,snippet", $video);

    $media = new \Google_Http_MediaFileUpload(
        $this->client,
        $insertRequest,
        'video/*',
        null,
        true,
        $chunkSizeBytes
    );
    $media->setFileSize(filesize($filePath));

    // Read the media file and upload it chunk by chunk.
    $status = false;
    $handle = fopen($filePath, "rb");
    while (!$status && !feof($handle)) {
        $chunk = fread($handle, $chunkSizeBytes);
        $status = $media->nextChunk($chunk);
    }
    fclose($handle);

Response error

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

saranshdhingra commented 11 months ago

Hi @padi-dev-lucvt Thanks for filing the issue.

I used the exact same code as your provided with different versions of the api client (including 2.13) and I was able to upload the video without any issues.

If you're still facing this issue, I would recommend trying different video files, changing the file paths and changing the environment(for example try the code in a fresh docker container).

The code you've shown is completely correct.

I'll be closing this issue for now, but if for some reason you're having troubles, please feel free to reopen this. Thanks :)