一、申请成为服务商,对金山文档在线服务进行申请
①进入官网 https://open.wps.cn/
②申请后如下图,点击右下角的进入服务
③申请成功后
④数据回调URL一定是服务器地址,本次我使用的是域名地址
完成上述步骤,就可以准备开发了
二、官方的技术文档一定要阅读
①简单介绍下运行流程
②我们需要做的,是在自己的应用程序中添加官方文档需要的接口如下图
③技术文档地址 http://open-doc.wps.cn/金山文档在线编辑/快速接入.html
三、了解运行机制以及需要的接口后,开始愉快的编码
本次的教程为demo,目的是为了能打开我们私有云存储的office文档,仅支持打开文档,其他功能后续编码。
示例代码仅提供 /v1/3rd/file/info 接口,其他的接口参照官方文档进行开发即可
①首先需要进行签名生成,每次打开文档的url都需要进行获取签名,签名是按照参数进行生成的
签名规则一定要参照官网文档,官方给的是JAVA版本,我们.NET的代码参考了其他博主的代码,整理后如下
/// <summary>
/// 签名规则
/// </summary>
/// <param name="paras">参数</param>
/// <param name="appId">申请的APPID</param>
/// <param name="appSecret">申请的APPKEY</param>
/// <returns></returns>
public string Sign(Dictionary<string,string> paras,string appId,string appSecret) {string paraValue = string.Empty;
//按照字典key进行排序
List<string> keys = paras.Keys.OrderBy(x=>x).ToList();
foreach (var key in keys)
{
//过滤APPKEY参数,最后加上
if (key == "_w_secretkey")
{
continue;
}
paraValue += key + "="+paras[key];
}
//APPKEY参数,最后加上
paraValue += "_w_secretkey=" + paras["_w_secretkey"];
var hmacshal = new HMACSHA1(Encoding.UTF8.GetBytes(appSecret));
var dataBuffer = Encoding.UTF8.GetBytes(paraValue);
var hashBytes = hmacshal.ComputeHash(dataBuffer);
string base64 = Convert.ToBase64String(hashBytes);
return HttpUtility.UrlEncode(base64);
}
测试,官网为例
官方提供的如下
"contents": "_w_appid=123456_w_fname=222.docx_w_userid=id1000_w_secretkey=7890"
"signature": "dL3ce9l0l+GKTz+i0R++H2qWwrQ=" (未URL编码)
我们需要做的,将contents部分切割成 Dictionary<string, string>传入
测试代码如下,调用上面的签名代码
public string test1()
{
Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("_w_appid", "");
d.Add("_w_fname", "222.docx");
d.Add("_w_userid", "id1000");
d.Add("_w_secretkey", "");
return Sign(d, "", "");
}
调用后生成的签名应该是和官网一致,官方的是未URL编码
我们上述Sign方法已进行URL编码,结果为 dL3ce9l0l+GKTz+i0R++H2qWwrQ%3d
特别注意,这只是我们的demo程序,非正式应用,所以真实运行场景一定不是我们这样子随意编码
②项目如果是普通的MVC,需要修改RouteConfig,启用Attribute路由,以实现 回调方法 如 /v1/3rd/file/info 这种形式(如果采用其他方式可忽略)
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapMvcAttributeRoutes();//启用Attribute路由 routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}③ 接口 /v1/3rd/file/info 代码
/// <summary>
/// 获取文件元数据
/// </summary>
/// <returns></returns>
[Route("v1/3rd/file/info")]
[HttpGet]
public JsonResult info(string _w_signature,string _w_appid)
{
var res = new JsonResult();
response_wpsModel wpsModel = new response_wpsModel(); userModel user = new userModel();
user.id = "";
user.name = "kls-dev";//用户权限
user.permission = "write";
user.avatar_url = "http://XXXXX.com/图片20180813135932.jpg";
fileModel f = new fileModel();//文件主键,要确保唯一
f.id = "id2019110609261";
f.name = "文件名.xlsx";
f.version = ;
f.size = ;
f.creator = user.name;
f.create_time= long.Parse(TimeHelper.GetCurrentTimestamp(false));
f.modifier = user.name;
f.modify_time =long.Parse(TimeHelper.GetCurrentTimestamp(false)) ;//文件的云地址
f.download_url = "http://XXXXX.com/Content/文件名.xlsx";
user_aclModel acl = new user_aclModel();
acl.rename = ;
acl.history = ;
f.user_acl = acl;
f.watermark =new watermarkModel(); wpsModel.file = f;
wpsModel.user = user;
return Json(wpsModel,JsonRequestBehavior.AllowGet);
} //补充时间戳帮助类/// <summary>
/// 时间相关
/// </summary>
public static class TimeHelper
{
/// <summary>
/// 获取当前时间戳
/// </summary>
/// <param name="millisecond">精度(毫秒)设置 true,则生成13位的时间戳;精度(秒)设置为 false,则生成10位的时间戳;默认为 true </param>
/// <returns></returns>
public static string GetCurrentTimestamp(bool millisecond = true)
{
return DateTime.Now.ToTimestamp(millisecond);
}/// <summary>
/// 转换指定时间得到对应的时间戳
/// </summary>
/// <param name="dateTime"></param>
/// <param name="millisecond">精度(毫秒)设置 true,则生成13位的时间戳;精度(秒)设置为 false,则生成10位的时间戳;默认为 true </param>
/// <returns>返回对应的时间戳</returns>
public static string ToTimestamp(this DateTime dateTime, bool millisecond = true)
{
return dateTime.ToTimestampLong(millisecond).ToString();
}/// <summary>
/// 转换指定时间得到对应的时间戳
/// </summary>
/// <param name="dateTime"></param>
/// <param name="millisecond">精度(毫秒)设置 true,则生成13位的时间戳;精度(秒)设置为 false,则生成10位的时间戳;默认为 true </param>
/// <returns>返回对应的时间戳</returns>
public static long ToTimestampLong(this DateTime dateTime, bool millisecond = true)
{
var ts = dateTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return millisecond ? Convert.ToInt64(ts.TotalMilliseconds) : Convert.ToInt64(ts.TotalSeconds);
}/// <summary>
/// 转换指定时间戳到对应的时间
/// </summary>
/// <param name="timestamp">(10位或13位)时间戳</param>
/// <returns>返回对应的时间</returns>
public static DateTime ToDateTime(this string timestamp)
{
var tz = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, 0));
return timestamp.Length == 13
? tz.AddMilliseconds(Convert.ToInt64(timestamp))
: tz.AddSeconds(Convert.ToInt64(timestamp));
}
}
④需要用到的实体类
/// <summary>
/// 返回的实体
/// </summary>
public class response_wpsModel {
public fileModel file { get; set; }
public userModel user { get; set; }
}
/// <summary>
/// 文件属性
/// </summary>
public class fileModel
{
public string id { get; set; }
public string name { get; set; }
public int version { get; set; }
public int size { get; set; }
public string creator { get; set; }
public long create_time { get; set; }
public string modifier { get; set; }
public long modify_time { get; set; }
public string download_url { get; set; }
public user_aclModel user_acl { get; set; }
public watermarkModel watermark { get; set; } }
/// <summary>
/// 用户权限
/// </summary>
public class user_aclModel
{
public int rename { get; set; }
public int history { get; set; }
}
/// <summary>
/// 水印
/// </summary>
public class watermarkModel
{
private static int _type = ;
public int type {
get { return _type; }
set { _type = value;}
}
private static string _value = "公司名称";
public string value
{
get { return _value; }
set { _value = value; }
}
private static string _fillstyle = "rgba( 192, 192, 192, 0.6)";
public string fillstyle
{
get { return _fillstyle; }
set { _fillstyle = value; }
}
private static string _font = "bold 20px Serif";
public string font
{
get { return _font; }
set { _font = value; }
}
private static double _rotate = -0.7853982;
public double rotate
{
get { return _rotate; }
set { _rotate = value; }
}
private static int _horizontal = ;
public int horizontal
{
get { return _horizontal; }
set { _horizontal = value; }
}
private static int _vertical = ;
public int vertical
{
get { return _vertical; }
set { _vertical = value; }
}
}
/// <summary>
/// wps用户实体
/// </summary>
public class userModel {
public string id { get; set; }
public string name { get; set; }
public string permission { get; set; }
public string avatar_url { get; set; }
}
⑤程序发布到服务器后,在线打开我们的文档,下面的url是Excel的,具体的还是要参考官方文档
好了,可以愉快的在线打开office文档了。