Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload video on Youtube using curl and api v3

I would upload a video using the Youtube API v3 with curl in PHP, as described here: https://developers.google.com/youtube/v3/docs/videos/insert

I've this function

function uploadVideo($file, $title, $description, $tags, $categoryId, $privacy)
{
    $token = getToken(); // Tested function to retrieve the correct AuthToken

    $video->snippet['title']         = $title;
    $video->snippet['description']   = $description;
    $video->snippet['categoryId']    = $categoryId;
    $video->snippet['tags']          = $tags; // array
    $video->snippet['privacyStatus'] = $privacy;
    $res = json_encode($video);

    $parms = array(
        'part'  => 'snippet',
        'file'  => '@'.$_SERVER['DOCUMENT_ROOT'].'/complete/path/to/'.$file
        'video' => $res
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://www.googleapis.com/upload/youtube/v3/videos');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $parms);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$token['access_token']));
    $return = json_decode(curl_exec($ch));
    curl_close($ch);

    return $return;
}

But it returns this

stdClass Object
(
    [error] => stdClass Object
        (
            [errors] => Array
                (
                    [0] => stdClass Object
                        (
                            [domain] => global
                            [reason] => badContent
                            [message] => Unsupported content with type: application/octet-stream
                        )

                )

            [code] => 400
            [message] => Unsupported content with type: application/octet-stream
        )

)

The file is an MP4.

Anyone can help?

like image 444
genna Avatar asked Nov 07 '12 16:11

genna


1 Answers

Updated version: Now with custom upload url and sending of metadata with the upload process. The entire process requires 2 requests:

  1. Get a custom upload location

    First, make a POST request for an upload url to:

    "https://www.googleapis.com/upload/youtube/v3/videos"
    

    You will need to send 2 headers:

    "Authorization": "Bearer {YOUR_ACCESS_TOKEN}"
    "Content-type": "application/json"
    

    You need to send 3 parameters:

    "uploadType": "resumable"
    "part": "snippet, status"
    "key": {YOUR_API_KEY}
    

    And you will need to send the metadata for the video in the request body:

        {
            "snippet": {
                "title": {VIDEO TITLE},
                "description": {VIDEO DESCRIPTION},
                "tags": [{TAGS LIST}],
                "categoryId": {YOUTUBE CATEGORY ID}
            },
            "status": {
                "privacyStatus": {"public", "unlisted" OR "private"}
            }
        }
    

    From this request you should get a response with a "location" field in the headers.

  2. POST to custom location to send file.

    For the upload you need 1 header:

    "Authorization": "Bearer {YOUR_ACCESS_TOKEN}"
    

    And send the file as your data/body.

If you read through how their client works you will see they recommend retrying if you are returned errors of code 500, 502, 503, or 504. Clearly you will want to have a wait period between retries and a max number of retries. It works in my system every time, though I am using python & urllib2 instead of cURL.

Also, because of the custom upload location this version is upload resumable capable, though I have yet to need that.

like image 161
Chad Befus Avatar answered Oct 15 '22 23:10

Chad Befus