标签:
优酷第三方上传API没有.NET版本的SDK,让从事.NET开发人员要实现开放平台上传文件无从下手。本文经过一天的预读优酷文档,以NET方式实现了视频上传。
参考:
优酷开放文档 http://open.youku.com/
强烈鄙视优酷团队既然没有NET版本SDK
本来以为可以通过合作级别授权,文档里面说通过合作级别授权自需要用户名和密码,然后去申请合作者身份,优酷客服告知不允许申请。
哎,只能硬着头皮去用NET采用通用授权方式去实现。
通用授权思路:
1、请求用户授权,跳出页面优酷登入授权页面。运行授权后获取授权Code
2、通过授权Code换取AccessToken,完成授权。
3、然后利用AccessToken进行js上传视频。
下面是上传demo的实现代码
public class HomeController : Controller { private string ClidentID = "您的clientid"; private string ClientSecret = "28938bb551381999eb5e365d429e19a7"; private string AccessTokenUrl = "https://openapi.youku.com/v2/oauth2/token"; private string Code = ""; }
1、跳转到授权页面
public ActionResult Oauth() { var url = "https://openapi.youku.com/v2/oauth2/authorize?client_id=你的ClientID&response_type=code&redirect_uri=http://您的域名/youku/home/Callback"; return Redirect(url); }
2、Callback获取Code
public ActionResult Callback(string code, string state) { Session.Add("Code", code); // 这里为了演示暂时采用session进行存储。实际情况可以存在文件或者数据库中。 FileLogHelper.WriteLog(code); return Content(code + "...........State=" + state); }
3、通过Code获取AccessToken
public ActionResult Index() { ViewBag.ClientId = ClidentID; if (Session["Code"] != null) { Code = (string) Session["Code"]; FileLogHelper.WriteLog("session=" + Code); ViewBag.AccessToken = GetAccessToken(); } else { ViewBag.AccessToken = GetAccessToken(); } return View(); }
GetAccessToken()方法
private string GetAccessToken() { if (Session["AccessToken"] == null) { var stringADict = new Dictionary<string, string>(); stringADict.Add("client_id", ClidentID); stringADict.Add("client_secret", ClientSecret); stringADict.Add("grant_type", "authorization_code"); stringADict.Add("code", Code); stringADict.Add("redirect_uri", "http://www.forfutures.com:33366/youku/home/Callback"); var result = Post.PostGetJson<AccessToken>(AccessTokenUrl, null, stringADict); Session["AccessToken"] = result.access_token; return result.access_token; } return Session["AccessToken"] as string; }
Index.chtml
@{ Layout = null; } <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML> <HEAD> <TITLE> New Document </TITLE> <META name="Generator" content="EditPlus"> <META name="Author" content=""> <META name="Keywords" content=""> <META name="Description" content=""> <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> </HEAD> <link href="http://open.youku.com/assets/lib/bootstrap2.1.0/css/bootstrap.css" rel="stylesheet"> <link href="http://open.youku.com/assets/lib/bootstrap2.1.0/css/bootstrap-responsive.css" rel="stylesheet"> <script src="http://open.youku.com/assets/lib/jquery-1.8.1.min.js"></script> <script src="http://open.youku.com/assets/lib/uploadjs.php"></script> <BODY> <div id="youku-upload"> <div class="container"> <form class="well form-horizontal" name="video-upload"> <fieldset> <div class="control-group"> <label class="control-label" for="spanSWFUploadButton">选择文件:</label> <div id="uploadControl" class="controls"></div> </div> <div class="control-group"> <label class="control-label" for="input01">标题:</label> <div class="controls"> <input type="text" class="input-xlarge" id="input01" name="title"> </div> </div> <div class="control-group"> <label class="control-label" for="textarea">简介:</label> <div class="controls"> <textarea class="input-xlarge" id="textarea" rows="3" name="description"></textarea> </div> </div> <div class="control-group"> <label class="control-label" for="input02">标签:</label> <div class="controls"> <input type="text" class="input-xlarge" id="input02" name="tags"> <span class="help-inline"></span> </div> </div> <div class="control-group"> <label class="control-label" for="category-node">类别:</label> <div class="controls"> <select id="category-node" name="category" ></select> </div> </div> <div class="control-group"> <label class="control-label">版权所有</label> <div class="controls"> <label class="radio inline"> <input type="radio" name="copyright_type" id="copyright_type2" value="original" checked="">原创 </label> <label class="radio inline"> <input type="radio" name="copyright_type" id="copyright_type1" value="reproduced">转载 </label> </div> </div> <div class="control-group"> <label class="control-label">视频权限</label> <div class="controls"> <label class="radio inline"> <input type="radio" name="public_type" id="public_type1" value="all" checked="">公开 </label> <label class="radio inline"> <input type="radio" name="public_type" id="public_type2" value="friend">仅好友 </label> <label class="radio inline"> <input type="radio" name="public_type" id="public_type3" value="password">输入密码观看 </label> <label class="radio inline" style="display:none" id="passwrod"> <input type="text" class="input "name="watch_password"> </label> </div> </div> <div class="form-actions"> <button type="submit" class="btn btn-primary start" id="btn-upload-start"> <i class="icon-upload icon-white"></i> <span>开始上传</span> </button> </div> </fieldset> </form> <div class="row" > <div class="span5" id="upload-status-wraper" ></div> </div> <br> <div class="well"><h3>说明</h3><ul><li>最大支持上传<strong>1 GB</strong> 视频文件</li><li>允许上传的视频格式为:wmv,avi,dat,asf,rm,rmvb,ram,mpg,mpeg,3gp,mov,mp4,m4v,dvix,dv,dat,</br>mkv,flv,vob,ram,qt,divx,cpk,fli,flc,mod。不符合格式的视频将会被丢弃,请确保视频格式的正确性,避免上传失败</li><li></li></ul> </div> </div> <!--完成上传的DOM和登录DOM 开始--> <div id="complete"></div> <div id="login" style="width:100%;height:100%;position:fixed;z-index:999;left:0px;top:0px;overflow:hidden;display:none;"> </div> <!--完成上传的DOM和登录DOM 结束--> </BODY> <script> var USE_STREAM_UPLOAD = true; jQuery(document).ready(function () { var param = { client_id: " 8fb430c56b72c78c", access_token: "@ViewBag.AccessToken", oauth_opentype: "iframe", oauth_redirect_uri: "http://www.forfutures.com:33366/youku/home/Oauth", oauth_state: "", completeCallback: "uploadComplete", categoryCallback: "categoryLoaded" }; youkuUploadInit(param); }); //上传完成时回调方法 function uploadComplete(data) { alert("videoid=" + data.videoid + ";title=" + data.title); } //分类加载后回调方法 function categoryLoaded(data) { if (data.categories) { var tpl = ‘‘; for (var i = 0; i < data.categories.length; i++) { if (data.categories[i].term == ‘Ads‘) { tpl += ‘<option value="‘ + data.categories[i].term + ‘" selected>‘ + data.categories[i].label + ‘</option>‘; } else { tpl += ‘<option value="‘ + data.categories[i].term + ‘" >‘ + data.categories[i].label + ‘</option>‘; } } $("#category-node").html(tpl); } } </script> </HTML>
以上代码就基本上实现优酷视频的上传。
但是还有个问题需要处理,就是AccessToken好像30天就会过期,我们应该在比如29天的时候通过刷新获取新的令牌 Refresh Access Token。这样就可以实现每次都进行授权认证的页面。
标签:
原文地址:http://www.cnblogs.com/DomoYao/p/4827205.html