This page explains how to upload videos to YouTube using direct uploading. Direct uploading lets you add videos that are in your video library to YouTube. You should choose a direct-upload implementation if you want to host or store videos uploaded through your site and also add those videos to YouTube. In a direct-uploading scenario, when a user uploads a video through your site, the video will be sent to your servers. Your application will subsequently send an API request to upload the video from your server to YouTube.
This page contains the following sections:
Sending an Upload API Request
To upload a video, send a POST request containing the video and associated metadata to http://uploads.gdata.youtube.com/feeds/api/users/default/uploads. Your request will upload the video to the currently logged-in user's account. The authentication token that you upload with the request identifies that user.
The Upload API request must have the following format:
POST /feeds/api/users/default/uploads HTTP/1.1 Host: uploads.gdata.youtube.com Authorization: Bearer ACCESS_TOKEN GData-Version: 2 X-GData-Key: key=DEVELOPER_KEY Slug: VIDEO_FILENAME Content-Type: multipart/related; boundary="BOUNDARY_STRING" Content-Length: CONTENT_LENGTH Connection: close --<boundary_string> Content-Type: application/atom+xml; charset=UTF-8 API_XML_request --BOUNDARY_STRING Content-Type: VIDEO_CONTENT_TYPE Content-Transfer-Encoding: binary <Binary File Data> --BOUNDARY_STRING--
Note: The hostname for uploading a video via the API, uploads.gdata.youtube.com, is different than the hostname used for all other API functions. However, you can use the same authentication token for uploading as for other API functions.
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:
YOUTUBE_USERNAME - Note that this variable is only relevant for requests that use ClientLogin authentication. This value contains the username for the user's YouTube account. The uploaded video will be associated with this account.
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.
VIDEO_FILENAME - This value contains the name of the video file that the content creator is uploading.
BOUNDARY_STRING - This value contains a string of non-space characters. Typically, the string is a series of hyphens followed by a random set of alphanumeric characters. Note: This value appears multiple times in the POST request.
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.
VIDEO_CONTENT_TYPE - This value contains the MIME type of the uploaded video file. The MIME type can be a video media type, such as
video/mp4, or it can be
Binary File Data - This value contains the binary code for the video file that is being uploaded.
The following example shows a POST request that has all of these values populated with the exception of the access (authentication) token and the binary file data:
POST /feeds/api/users/default/uploads HTTP/1.1 Host: uploads.gdata.youtube.com Authorization: Bearer ACCESS_TOKEN GData-Version: 2 X-GData-Key: key=adf15ee97731bca89da876c...a8dc Slug: video-test.mp4 Content-Type: multipart/related; boundary="f93dcbA3" Content-Length: 1941255 Connection: close --f93dcbA3 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> --f93dcbA3 Content-Type: video/mp4 Content-Transfer-Encoding: binary <Binary File Data> --f93dcbA3--
Handling the Upload API Response
When you submit an Upload API request, the API returns an Atom entry that contains information about the uploaded video. The entry has the same format as an entry that would appear in a user's uploaded videos feed. The response may contain properly escaped HTML tags.
The entry will contain one <link> tag for which the value of the rel attribute is self. To check the status of the uploaded video, send a GET request to the URL identified in this <link> tag. See the following section, Checking the status of an uploaded video, for more information.
Process Flow Diagram
The following diagram illustrates the direct 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 and select the actual video file to upload.
The user selects the video file and 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 as well as the video file and the video details that the user submitted in the previous step.
YouTube returns an API response describing the new video.
You display a confirmation to the user that the video was uploaded successfully.