优酷第三方上传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的上传的更多相关文章

  1. ASP.NET MVC中,怎么使用jquery/ajaxForm上传文件

    ajaxForm插件最好选择:jquery forms plugin. 以下为示例: Ajax.BeginForm @using (Ajax.BeginForm("YourAction&qu ...

  2. 在MVC应用程序中,怎样删除上传的文件

    在ASP.NET MVC应用程序中,怎样删除上传的文件. 由于上传时,真正文件是存储在应用程序某一目录,在数据库表中,只是存储其基本信息.在删除时,需要注意一下,由于没有事务可操作.Insus.NET ...

  3. 返璞归真 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 上传文件, ...

  4. ASP.NET MVC 提供与訪问 Web Api

    ASP.NET MVC 提供与訪问 Web Api 一.提供一个 Web Api 新建一个项目.类型就选 "Web Api". 我用的是MVC5,结果生成的项目一大堆东西.还编译只 ...

  5. ASP.NET 开发必备知识点(1):如何让Asp.net网站运行在自定义的Web服务器上

    一.前言 大家都知道,在之前,我们Asp.net 的网站都只能部署在IIS上,并且IIS也只存在于Windows上,这样Asp.net开发的网站就难以做到跨平台.由于微软的各项技术的开源,所以微软自然 ...

  6. WEB文件上传下载功能

    WEB文件上传下载在日常工作中经常用到的功能 这里用到JS库 http://files.cnblogs.com/meilibao/ajaxupload.3.5.js 上传代码段(HTML) <% ...

  7. 2014-07-23 利用ASP.NET自带控件实现单文件上传与下载

    效果图 上传文件页面: 下载文件页面:  1.母版页site.Master <%@ Master Language="C#" AutoEventWireup="tr ...

  8. Java Web文件上传原理分析(不借助开源fileupload上传jar包)

    Java Web文件上传原理分析(不借助开源fileupload上传jar包) 博客分类: Java Web   最近在面试IBM时,面试官突然问到:如果让你自己实现一个文件上传,你的代码要如何写,不 ...

  9. web service上传参数代码实例

    web service上传参数代码实例 这次做的项目用到webservice比较多,最开始在网上看的参考dome,发现都不行,后来发现安卓4.0以后有很大的不同,在做传参时,有些东西需要注意: 第一, ...

随机推荐

  1. TensorFlow 辨异 —— tf.add(a, b) 与 a+b(tf.assign 与 =)、tf.nn.bias_add 与 tf.add

    1. tf.add(a, b) 与 a+b 在神经网络前向传播的过程中,经常可见如下两种形式的代码: tf.add(tf.matmul(x, w), b) tf.matmul(x, w) + b 简而 ...

  2. bat文件从@含义起

    今天看到一个批处理文件,内容很简单,执行很方便,学习了一下才知道就是一条条的dos命令, 掌握其中的几个常用命令能看懂别人的文件就行了 1.@ 一般紧随其后 类似@echo off 其作用类似于ech ...

  3. Popup 解决位置不随窗口/元素FrameworkElement 移动更新的问题

    原文:Popup 解决位置不随窗口/元素FrameworkElement 移动更新的问题 Popup弹出后,因业务需求设置了StaysOpen=true后,移动窗口位置或者改变窗口大小,Popup的位 ...

  4. IWXAPI的使用,发布分享和支付

    今天看代码,看到以前项目的微信支付功能,想做一下记录 首先是在application类里面定义 public static IWXAPI MSGAPI; public static final Str ...

  5. java 读取项目外面配置文件的方法

    public static void loadProps(String propertiesUrl) { props = new Properties(); InputStream in = null ...

  6. Nginx得知——流程模型(worker流程)

    流程模型 worker流程 master进程模型核心函数ngx_master_process_cycle()中调用了创建子进程函数ngx_start_worker_processes(),该函数源代码 ...

  7. laravel routes除了默认路由,其他的都无效 解决方案

    按照教程.该php升级到5.5,所有是开放的扩展,默认路由进入,证明代码错误,平时不开rewrite铅 假设你其它路由,localhost/文件夹/public/index.php/home能够进去. ...

  8. Delphi程序员如何找到高薪的工作?(赚不到钱,原因只有一个,就是他们没有被公司录取。Delphi必须要独自进行深入研究,才能精通,同时也不能自由性太强)

    转帖自:http://www.tommstudio.com/ViewNews.aspx?ID=187http://hi.baidu.com/rarnu/blog/ 本文翻译自<美国优秀经理观念大 ...

  9. 设置m_pszAppName值的做法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 CWinApp::m_pszAppName用于指定应用程序的名字.昨天这样修改它的值: m_pszAppName = ...

  10. OpenCV图像修复

    在OpenCV的"photo.hpp"中定义了一个inpaint函数,可以用来实现图像的修复和复原功能,inpaint函数的原型如下: void inpaint( InputArr ...