Please allow me to take you through the AuthSub of YouTube to upload videos using your php file.
This tutorial describes the direct browser based upload technique using AuthSub.
AuthSub is the Authorization module of the YouTube that lets your application interact with the YouTube for specific purposes such as Uploading videos etc on user’s behalf.It is same like Auth and a cousin of oAuth.
A user grants the privilege to your site application and you can do the job on his behalf as simple as that.
We will go through the way to upload a video using AuthSub.
It goes as follows and can be really done in following 4 simple steps:
1. To allow the application run on user behalf a user must have authorized it first.So our first step to implement is to get the app Authorized by the user.We do it by simply redirecting user to the authorization page the url is as follows:
$nextUrl = urlencode(‘http://www.xxxx.com’)
$scope = urlencode(‘http://gdata.youtube.com’);
The nextUrl here is the url of the your application where the user will be redirected after authorization procedure.
scope is to tell the YouTube about the scope of the process which is google data youtube in this case.
So if user has not authorized your app yet he must be redirected to the above mentioned authorization page once the user has approved your application it needs not to follow the step one ever again until and unless the user revokes the access to you app from the users control panel of his account.On successful completion of the authorization process user will be redirected to your application and this complete the first step of AuthSub.
2.If from the first step the user authenticates your application YouTube will redirect him back to your application with a token in the url.You are going to use this token and here is where the actual AuthSub process comes into play you are going to use this token to obtain an entity called AuthSubSessionToken which will allow you to interact your app to YouTube on the user behalf who has just approved your application.In PHP you do it by issuing a curl request.The details are as follows:
Issue a curl GET request to https://www.google.com/accounts/AuthSubSessionToken with the token you received just after the authorization step.Remember to turn ON the curl’s response gathering status as you gonna need that.If everything went well till now you would be responded from YouTube with the AuthSubSessionToken
3.Now when you have received the AuthSubSessionToken you are gonna use that to get an upload token which will actually upload the data related to your video to YouTube i.e.title,description,category and keywords.
This is kinda reverse process as in AuthSub you upload the data related to the video to YouTube first and then upload the video itself.The uploading of video data also referred as MetaData will be done by feeding XML to the YouTube,the xml will be:
title goes here
description goes here
category goes here
Keyword goes here
and again curl has bussiness to do you will upload this by issuing another curl call:
headers:AuthSub token =”Your AuthSubSession token goes here”
‘X-GData-Key: key=”Your Api key goes here”
Content-length: length of the xml you formed above goes here
POSTFIELDS: the xml itself that you formed
4.If the step 3 completes successfully then its time to upload the video actually on your successful last curl execution you will be reverted back by the YouTube with a url an a token
Now you will create a form which will have this url as its action and token as a hidden field something like this
Just select the video and click submit and your video will get uploaded.On successful submission you will be redirected back with status 200.
For any queries please comment on this post or mail us at any of the aforementioned email addresses.We will see you through.
The github link for the Sample code is here.
In case of a problem you can comment on this post for help
Please do remember to share us and give likes on your favorite Social Media and help us grow.