Note: The YouTube Data API (v2) has been officially deprecated as of March 4, 2014. Please refer to our deprecation policy for more information. Please use the YouTube Data API (v3) for new integrations and migrate applications still using the v2 API to the v3 API as well.
This page explains how to enable your application to let users upload videos to YouTube using browser-based uploading. This uploading method lets you accept video uploads from your users without having to handle, host or proxy the actual video files. You should choose the browser-based uploading approach if you do not want to host or store the uploaded video files.
The browser-based uploading process has three steps:
- Step 1 - Uploading video metadata
- Step 2 - Extracting values from the API response
- Step 3 - Uploading the video file
Step 1 - Uploading video metadata
The example below shows the format of an API request that uses the OAuth 2.0 authentication scheme for browser-based uploading:
POST /action/GetUploadToken HTTP/1.1 Host: gdata.youtube.com Authorization: Bearer ACCESS_TOKEN GData-Version: 2 X-GData-Key: key=DEVELOPER_KEY Content-Length: CONTENT_LENGTH Content-Type: application/atom+xml; charset=UTF-8 API_XML_request
Variables in the upload request
You must provide several values in the POST request. Those values are highlighted in bold text in the example above. The following list explains how to populate each value:
ACCESS_TOKEN - This value contains the authentication token for your request. The token will either be an OAuth 2.0 access token, an OAuth 1.0 access token, a ClientLogin token, an AuthSub single-use token, or an AuthSub session token.
DEVELOPER_KEY - This value uniquely identifies the application that is submitting the request to upload the video. Please visit http://code.google.com/apis/youtube/dashboard/ to get a developer key.
CONTENT_LENGTH - This value contains the length, in bytes, of the entire body of the POST request.
API_XML_Request - This value contains information about the uploaded video file in the form of an Atom XML entry. The example below shows a sample XML request. The XML tags used in the entry are defined in the Reference Guide.
The following example shows a POST request that has all of these values populated with the exception of the access (authentication) token:
POST /action/GetUploadToken HTTP/1.1 Host: gdata.youtube.com Authorization: Bearer ACCESS_TOKEN GData-Version: 2 X-GData-Key: key=adf15ee97731bca89da876c...a8dc Content-Length: 1941255 Content-Type: application/atom+xml; charset=UTF-8 <?xml version="1.0"?> <entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007"> <media:group> <media:title type="plain">Bad Wedding Toast</media:title> <media:description type="plain"> I gave a bad toast at my friend's wedding. </media:description> <media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People </media:category> <media:keywords>toast, wedding</media:keywords> </media:group> </entry>
Step 2 - Extracting values from the API response
When you submit an Upload API request, the API returns an XML response that contains an upload URL and upload token that enable the user to upload the actual video file. You will need to extract the URL and token from the response and include these values in the form on your web page where the user submits the video file.
The example below shows a sample API response to a request for browser-based uploading:
<?xml version='1.0' encoding='UTF-8'?> <response> <url>http://uploads.gdata.youtube.com/action/FormDataUpload/AEF3087AUD<url> <token>AEwbFAQEvf3xox...</token> </response>
Step 3 - Uploading the video file
After extracting the upload URL and upload token from the API response, you need to display a form so that the user can upload the actual video file. The form must use the upload URL as the value of the <form> tag's action attribute and have a hidden input field containing the upload token. In addition, the form should verify that the user has selected a file to upload before allowing the user to actually submit the form.
Please note that the form must follow these guidelines:
You need to add the nexturl parameter to the form's target URL. This parameter specifies the URL to which YouTube will redirect the user's browser when the user uploads his video file. After the video is uploaded in the browser, the user will be redirected to the nexturl URL. See the reference guide for more information about additional parameters that will be appended to the URL.
You must set the value of the <form> tag's enctype attribute to multipart/form-data.
The <input> tag that identifies the file must be named file.
The <input> tag that contains the token must be named token.
Your application should verify that the user has selected a file to upload before allowing the user to submit the form to upload the file.
Process Flow Diagram
The following diagram illustrates the browser-based uploading process. Your application might have already completed the authentication process before these steps occur. However, you could also choose to have the user provide the video metadata before completing the authentication process.
The image shows the following steps:
Your site displays a form in which the user will enter details about the video being uploaded.
The user enters the video details, such as the title, description and category for the video.
Your site sends a POST request to YouTube that contains the authentication token and the video details that the user submitted in the previous step.
YouTube returns a one-time upload token, an encrypted value containing authentication information and video details for the video being uploaded. The Extracting values from the API response section identifies the upload token and URL in the API response.
Your site displays a form where the user can select his video file and upload it to YouTube. The form will submit directly to YouTube and must contain a hidden input field that specifies the upload token obtained in the previous step. The Uploading the video file section defines the guidelines for this form.
The user selects his video and submits the form, sending his video and the upload token directly to YouTube. YouTube verifies the token is valid and adds the video to the user's YouTube channel. During this process, YouTube assigns the video a unique ID, which will be used to identify the video on YouTube.
YouTube redirects the user back to your site.