You will need need Client Id and Developer Key for using Google's data service API, you can apply it from above link. There is also sample code available for reference for .NET developers - http://code.google.com/apis/youtube/code.html#sample_code
Here is the code snipped for uploading video to YouTube:
Web Part User Interface Button Upload Code:
FileUploadField.Value = FileUrl;
oVideoUploadData = new VideoUploadData();
Stream iStream = VideoFileUpload.PostedFile.InputStream;
oVideoUploadData.UploadToYouTube([Title], [Description], [Category], [Keywords],
iStream, [FileName], _listName, sSiteUrl, sWebUrl);
///
/// Method to upload the video to YouTube from the webpart page
///
public bool UploadToYouTube(string sTitle, string sDescription, string sCategory, string sKeywords, Stream iStream, string fname, string ListName, string SiteUrl, string WebUrl) {
try
{
SPSite CurrentSite = new SPSite(SiteUrl);
SPWeb CurrentWeb = null;
CurrentWeb = CurrentSite.OpenWeb(WebUrl);
YouTubeEntry VideoEntry = new YouTubeEntry();
VideoEntry.Media = new MediaGroup();
VideoEntry.Media.ExtensionElements.Add(new Private());
VideoEntry.Media.Description = new MediaDescription(sDescription);
VideoEntry.Media.Title = new MediaTitle(sTitle);
VideoEntry.Media.Keywords = new MediaKeywords(sKeywords);
MediaCategory category = new MediaCategory(sCategory);
category.Attributes["scheme"] = YouTubeService.DefaultCategory;
VideoEntry.Media.Categories.Add(category);
VideoEntry.MediaSource = new MediaFileSource(iStream, fname, "video/quicktime");
YouTubeEntry createdEntry = service.Upload(VideoEntry);
string sUri = createdEntry.Id.AbsoluteUri;
string[] SlashSplitArray = sUri.Split('/');
string sVideoId = SlashSplitArray[SlashSplitArray.Length - 1].ToString();
CurrentWeb.AllowUnsafeUpdates = true;
SPList CurrentList = CurrentWeb.Lists["" + ListName + ""];
SPListItem VideoItem = CurrentList.Items.Add();
VideoItem["Title"] = sTitle;
VideoItem["Video ID"] = sVideoId;
VideoItem["Comments"] = sDescription;
VideoItem["Category"] = sCategory;
VideoItem["Keywords"] = sKeywords;
VideoItem["List ID"] = _listid;
VideoItem["Web ID"] = _webid;
VideoItem["List Item ID"] = _itemid;
VideoItem.Update();
_error = false;
// } }
catch (GDataRequestException gdre)
{
_exceptionOccured = gdre;
WriteToEventLog(gdre.Message, EventLogEntryType.Error, "Upload in YouTube");
_error = true;
}
catch (Exception ex)
{
_exceptionOccured = ex;
WriteToEventLog(ex.Message, EventLogEntryType.Error, "Upload in YouTube");
_error = true;
}
finally
{
_running = false;
}
return _error;
}
void SetService(SPWeb CurrentWeb)
{
try
{
SPList ShareTubeConfigurationList = CurrentWeb.Lists["ShareTube Configuration Settings List"];
SPQuery ConfigurationQuery = new SPQuery();
ConfigurationQuery.Query = "
SPListItemCollection ConfigurationItemCollection = ShareTubeConfigurationList.GetItems(ConfigurationQuery);
SPListItem ConfigurationItem = ConfigurationItemCollection[0];
_developerkey = ConfigurationItem["Developer Key"].ToString();
_configurationkey = ConfigurationItem["Client ID"].ToString();
_applicationTitle = ConfigurationItem["Title"].ToString();
_userName = ConfigurationItem["User Name"].ToString();
_password = ConfigurationItem["Password"].ToString();
_itemid = ConfigurationItem.ID.ToString();
_listid = ShareTubeConfigurationList.ID.ToString();
_webid = CurrentWeb.ID.ToString();
string PasswordString = null;
string Passwordtext = null;
string[] Passwordstr;
Passwordstr = _password.Split('#');
Passwordtext = Passwordstr[1];
Passwordtext = Passwordtext.Remove(Passwordtext.Length - 1, 1);
string Passwordstore = Passwordtext;
Security sec = new Security();
string DecPassword = sec.Decrypt(ref Passwordtext);
service = new YouTubeService(_applicationTitle, _configurationkey, _developerkey);
service.setUserCredentials(_userName, DecPassword);
}
catch (Exception ex)
{
WriteToEventLog(ex.Message, EventLogEntryType.Error, "SetService error");
}
}
1 comments → Using Youtube API in SharePoint Web Part
How do i use it ??
Post a Comment