Upload video on Youtube using curl and api v3 Upload video on Youtube using curl and api v3 curl curl

Upload video on Youtube using curl and api v3


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.


Unfortunately, we don't have a specific example of YouTube API v3 uploads from PHP available yet, but my general advice is:

  • Use the PHP client library instead of cURL.
  • Base your code on this example written for the Drive API. Because the YouTube API v3 shares a common API infrastructure with other Google APIs, examples for doing things like uploading files should be very similar across different services.
  • Take a look at the Python example for the specific metadata that needs to be set in a YouTube v3 upload.

In general, there are a lot of things incorrect with your cURL code, and I can't walk through all the steps it would take to fix it, as I think using the PHP client library is a much better option. If you are convinced you want to use cURL then I'll defer to someone else to provide specific guidance.


a python script:

# categoryId is '1' for Film & Animation# to fetch all categories: https://www.googleapis.com/youtube/v3/videoCategories?part=snippet&regionCode={2 chars region code}&key={app key}meta =  {'snippet': {'categoryId': '1',  'description': description,  'tags': ['any tag'],  'title': your_title},  'status': {'privacyStatus': 'private' if private else 'public'}}param = {'key': {GOOGLE_API_KEY},         'part': 'snippet,status',         'uploadType': 'resumable'}headers =  {'Authorization': 'Bearer {}'.format(token),           'Content-type': 'application/json'}#get location urlretries = 0retries_count = 1while retries <= retries_count:     requset = requests.request('POST', 'https://www.googleapis.com/upload/youtube/v3/videos',headers=headers,params=param,data=json.dumps(meta))    if requset.status_code in [500,503]:        retries += 1    breakif requset.status_code != 200:    #do somethinglocation = requset.headers['location']file_data = open(file_name, 'rb').read()headers =  {'Authorization': 'Bearer {}'.format(token)}#upload your videoretries = 0retries_count = 1while retries <= retries_count:    requset = requests.request('POST', location,headers=headers,data=file_data)    if requset.status_code in [500,503]:        retries += 1    breakif requset.status_code != 200:    #do something# get youtube idcont = json.loads(requset.content)            youtube_id = cont['id']