February 26, 2015 9:47 am

How to Authenticate & upload videos to YouTube Channel in PHP

We have received many requests from our readers to write tutorial on YouTube video uploading using data API, so today we are going to show you in this tutorial that how you can authenticate and upload videos on your YouTube channel in PHP. Basically we used YouTube Data API v3 in PHP and its very easy to configure demo and download source freely available.

How to Authenticate & upload videos to YouTube Channel in PHP

Setting up the environment

Step 1.

Go to Google’s Developer Console (https://console.developers.google.com/project)

Setting up the environment


Step 2.

Create a project

Setting up the environment 1

Step 3.

Open that project and click on Enable an API

Setting up the environment Enable API

Step 4.

Search for Youtube Data API v3 and turn it on (You could turn YouTube Data API on from APIs &auth -> APIs as well)

Setting up the environment enable youtube data API v3

Setting up the environment enable youtube data API v3 enabled

Step 5.

Go to Credentials under APIs and Create new Client ID

Setting up the environment API Credentials

Step 6.

Select client id application type

Setting up the environment API Credentials website

Step 7.

Write details for application

Setting up the environment API Credentials setup

Step 8.

Set up the JavaScript origins (websites that could access the API with your credentials). Do not forget to set up the REDIRECT URIs which should contain the full path to the page that is going to serve the API. You delimit different pages by new line (enter).

Setting up the environment API Credentials setup javascript origins

Step 9.

Use the CLIENT ID and CLIENT SECRET in the api.php file (replace $OAUTH2_CLIENT_ID with a string containing your client id and $OAUTH2_CLIENT_SECRET with a string containing your secret key.

Setting up the environment API Credentials copy

This app actually makes use of the Google’s API PHP client which is included along with the app itself in the downloads page.

Actual app


In the index we disable displaying errors and set the maximum execution time to 2 minutes (video uploads require more time than usual and the request may time out if the maximum execution time is low).

We set the video name and deduce the file format from that name. We have done this in order to show you how you can upload to users accounts different videos only by changing the $videoName variable (the app assumes they are stored in ‘videos’ directory relative to index.php)

We load api.php which is going to display modal urging users to authenticate with YouTube and will immediately start uploading then.

Towards the end, we display the name of the file that would be uploaded to the user’s account and add a video tag that renders the video in order for visitors to be certain they want this video.


We load the Google’s API PHP client files in an OS-independent way and start the session.

We set up the client id and the secret key.


We authenticate the user with his YouTube account.

If he is successfully authenticated, we set up the path to the video that we are going to upload for him and set metadata about the video (title, description and tags) and set up the category of the video

We send the actual video in chunks, enable resumable uploads and display the success text or the type of service/client error, if any.

If the user has not yet given authorization to the app, we add the modal to the DOM.


  1. Creative Commons Video downloaded from: http://youtu.be/ejjFWxPInGU
  2. Original Youtube Data API example: https://developers.google.com/youtube/v3/docs/videos/insert

Author Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter

Tutorial Categories:
  • Mark Bagnall

    Would be great to show from user selecting a video in a html form

  • popular design

    it’s to easy step, thank-you for sharing.

  • Awesome… thank you! it works! BTW, where is the api.php file?

    • huzoorbux

      Its available in our code please download to get that file.

  • Deepak Chandra Pandey

    could anyone tell me how to get refresh token , I have uploaded video on youtube using php after one hr the script giving me this error

    “An client error occurred: The OAuth 2.0 access token has
    expired, and a refresh token is not available. Refresh tokens are not
    returned for responses that were auto-approved.”

    Please tell me how to get refresh token

    • tinyCoder

      Same here, did you solve it please?

  • kekotamu

    How to make progress bar upload in this script?
    and how make a multiple upload?

  • Trang Hoàng

    not download???

  • Trang Hoàng

    not download???

  • nick

    i keep getting the authorization required message even after i am sent to youtube and confirmed everything. i get sent back to my page but authorization request shows up again and again

  • nick

    oh nevermind, it is working. the videos are being uploaded even though i get the error message.

  • sanjay

    how could I run script on cron job where we can’t authenticate account? I was previously doing with zend gdata library

  • Gil Palikaras

    Hello, I tried using this implementation (btw, works much better than the one in the official documentation!) but I am stuck in a very specific point:
    After I get the message that my video is uploaded, in the Video Manager page of my YouTube account I see the video, but the status message below it is “0% uploaded” and it stays like this.

    Also, I had to make a change to the code at line 22 to this:

    $media = new Google_Http_MediaFileUpload(

    Before that, with the original code, I got the message “Preparing upload.” (also stayed like this).
    I did change the credentials (client ID and Client Secret) to mine.

    Do you know what might be the reason that the upload can’t be completed?

    Thank you

  • Dhirendra Bisht

    Hi I am getting two errors 1st Its showing authorization popup in place of success message every time .

    And script working only once for one account 🙁 its realy urgent for me Thanks in advance for any suggestion This is my link http://jasonallan.work/videomaker/youtube/

  • Faizan Zahid

    Kindly update the blog with the directory structure of application so that some novice developer may find ease to understand it