Asp.Net MVC实现优酷(youku)Web的上传
优酷第三方上传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="" 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> 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 = ; 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。这样就可以实现每次都进行授权认证的页面。
Asp.Net MVC实现优酷(youku)Web的上传的更多相关文章
- ASP.NET MVC中,怎么使用jquery/ajaxForm上传文件
ajaxForm插件最好选择:jquery forms plugin. 以下为示例: Ajax.BeginForm @using (Ajax.BeginForm("YourAction&qu ...
- 在MVC应用程序中,怎样删除上传的文件
在ASP.NET MVC应用程序中,怎样删除上传的文件. 由于上传时,真正文件是存储在应用程序某一目录,在数据库表中,只是存储其基本信息.在删除时,需要注意一下,由于没有事务可操作.Insus.NET ...
- 返璞归真 asp.net mvc (11) - asp.net mvc 4.0 新特性之自宿主 Web API, 在 WebForm 中提供 Web API, 通过 Web API 上传文件, .net 4.5 带来的更方便的异步操作
原文:返璞归真 asp.net mvc (11) - asp.net mvc 4.0 新特性之自宿主 Web API, 在 WebForm 中提供 Web API, 通过 Web API 上传文件, ...
- ASP.NET MVC 提供与訪问 Web Api
ASP.NET MVC 提供与訪问 Web Api 一.提供一个 Web Api 新建一个项目.类型就选 "Web Api". 我用的是MVC5,结果生成的项目一大堆东西.还编译只 ...
- ASP.NET 开发必备知识点(1):如何让Asp.net网站运行在自定义的Web服务器上
一.前言 大家都知道,在之前,我们Asp.net 的网站都只能部署在IIS上,并且IIS也只存在于Windows上,这样Asp.net开发的网站就难以做到跨平台.由于微软的各项技术的开源,所以微软自然 ...
- WEB文件上传下载功能
WEB文件上传下载在日常工作中经常用到的功能 这里用到JS库 http://files.cnblogs.com/meilibao/ajaxupload.3.5.js 上传代码段(HTML) <% ...
- 2014-07-23 利用ASP.NET自带控件实现单文件上传与下载
效果图 上传文件页面: 下载文件页面: 1.母版页site.Master <%@ Master Language="C#" AutoEventWireup="tr ...
- Java Web文件上传原理分析(不借助开源fileupload上传jar包)
Java Web文件上传原理分析(不借助开源fileupload上传jar包) 博客分类: Java Web 最近在面试IBM时,面试官突然问到:如果让你自己实现一个文件上传,你的代码要如何写,不 ...
- web service上传参数代码实例
web service上传参数代码实例 这次做的项目用到webservice比较多,最开始在网上看的参考dome,发现都不行,后来发现安卓4.0以后有很大的不同,在做传参时,有些东西需要注意: 第一, ...
随机推荐
- Redis+Mysql模式和内存+硬盘模式的异同
http://www.open-open.com/lib/view/open1346029825942.html 学习任何新知识,都是一个循序渐进的过程,从刚开始的懵懂无知,到简单熟悉,然后突然的彻悟 ...
- [redis] redis配置文件redis.conf的详细说明
# Redis 配置文件 # 当配置中需要配置内存大小时,可以使用 1k, 5GB, 4M 等类似的格式,其转换方式如下(不区分大小写) # # 1k => bytes # 1kb => ...
- 一起学Python:网络通信过程
1. 2台电脑的网络 image 说明 如果两台电脑之间通过网线连接是可以直接通信的,但是需要提前设置好ip地址以及网络掩码 并且ip地址需要控制在同一网段内,例如 一台为192.168.1.1另一台 ...
- Springboot系列:@SpringBootApplication注解
在使用 Springboot 框架进行开发的时候,通常我们会在 main 函数上添加 @SpringBootApplication 注解,今天为大家解析一下 @SpringBootApplicatio ...
- 【33.33%】【codeforces 608C】Chain Reaction
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 生成动态Lambda表达式1
SqlDataReader生成动态Lambda表达式 上一扁使用动态lambda表达式来将DataTable转换成实体,比直接用反射快了不少.主要是首行转换的时候动态生成了委托. 后面的转换都是直接调 ...
- [Android]Fragment自定义动画、动画监听以及兼容性包使用
Fragment是Android在API 11之后加入的一个组件,对提高Android开发中的布局合理性和布局效率都有很大作用,尤其是在Android平板等大屏幕设备的开发中,Fragment的引入能 ...
- handsontable前端excel学习笔记
暂时没有好的中文资料,大概找了三遍随便看看,之后重点研究其github 1.Handsontable 学习笔记-Methods 2. Handsontable通用方法 3.handsontable的核 ...
- 从Client应用场景介绍IdentityServer4(五)
原文:从Client应用场景介绍IdentityServer4(五) 本节将在第四节基础上介绍如何实现IdentityServer4从数据库获取User进行验证,并对Claim进行权限设置. 一.新建 ...
- C# Winform在win10里弹出无焦点的窗口
原文:C# Winform在win10里弹出无焦点的窗口 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/wangmy1988/article/det ...