Publico el codigo de la solucion, YouTube ha cambiado todo y desde ahora solo se puede usar esa API mediante OAuth2, hay que obtener un RefreshToken que lo obtuve desde aqui StackOverFlow - Youtube api single user scenario with oauth uploading videos
Entonces, a continuación dejo el codigo de mi controlador completo, no olvidar cambiar las credenciales.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using testify.Models;
using testify.Clases;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Responses;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.YouTube.v3;
using Google.Apis.Services;
using Google.Apis.YouTube.v3.Data;
namespace testify.Controllers
{
public class PostsController : Controller
{
private TestifyContext db = new TestifyContext();
private string UploadVideoId;
// GET: Posts
public ActionResult Index()
{
return View(db.Posts.ToList());
}
// GET: Posts/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Post post = db.Posts.Find(id);
if (post == null)
{
return HttpNotFound();
}
return View(post);
}
// GET: Posts/Create
public ActionResult Create()
{
return View();
}
// POST: Posts/Create
// Para protegerse de ataques de publicación excesiva, habilite las propiedades específicas a las que desea enlazarse. Para obtener
// más información vea http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "PostId,Title,Description,videoId")] Post post, System.Web.Mvc.FormCollection collection, HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
db.Posts.Add(post);
db.SaveChanges();
UserCredential credential;
var token = new TokenResponse
{
RefreshToken = "insertarRefreshToken"
};
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
credential = new UserCredential(
new GoogleAuthorizationCodeFlow(
new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = GoogleClientSecrets.Load(stream).Secrets,
Scopes = new[]
{
YouTubeService.Scope.Youtube, YouTubeService.Scope.YoutubeUpload
},
}), "user",
token);
}
// create service
var service = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "test_app"
});
//objeto del video
var video = new Google.Apis.YouTube.v3.Data.Video();
video.Snippet = new VideoSnippet();
video.Snippet.Title = post.Title;
video.Snippet.Description = post.Description;
video.Snippet.Tags = new string[] { "tag1", "tag2", "Jesucristo" };
video.Snippet.DefaultAudioLanguage = "es";
video.Snippet.DefaultLanguage = "es";
video.Snippet.CategoryId = "22";
video.Status = new VideoStatus();
video.Status.PrivacyStatus = "unlisted";
//subida del video
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Content/Videos"), fileName);
file.SaveAs(path);
string filepath = Path.GetFullPath(path);
WebRequest req = WebRequest.Create(filepath);
using (Stream stream = req.GetResponse().GetResponseStream())
{
VideosResource.InsertMediaUpload insertRequest = service.Videos.Insert(video, "snippet, status", stream, "video/*");
insertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;
insertRequest.Upload();
}
// return Content(UploadVideoId);
//obtener el id del video
post.videoId = UploadVideoId;
//idVideoPost.videoId = video.Id;
db.Entry(post).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(post);
}
void videosInsertRequest_ResponseReceived(Video video)
{
// string.Format("{0}", video.Id);
UploadVideoId = video.Id;
}
// GET: Posts/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Post post = db.Posts.Find(id);
if (post == null)
{
return HttpNotFound();
}
return View(post);
}
// POST: Posts/Edit/5
// Para protegerse de ataques de publicación excesiva, habilite las propiedades específicas a las que desea enlazarse. Para obtener
// más información vea http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "PostId,Title,Description,videoId")] Post post)
{
if (ModelState.IsValid)
{
db.Entry(post).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(post);
}
// GET: Posts/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Post post = db.Posts.Find(id);
if (post == null)
{
return HttpNotFound();
}
return View(post);
}
// POST: Posts/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Post post = db.Posts.Find(id);
db.Posts.Remove(post);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}