钉钉是阿里巴巴专为中小企业和团队打造的沟通、协同的多端平台,钉钉开放平台旨在为企业提供更为丰富的办公协同解决方案。通过钉钉开放平台,企业或第三方合作伙伴可以帮助企业快速、低成本的实现高质量的移动微应用,实现生产、管理、协作、运营的移动化。官网的列子往往都是java,php和nodejs的,下面我用c#来实现一个简单的列子,来说明如何与钉钉进行交互:

  1 创建一个网站:

  

其中官网有如何进行账号注册等开发的准备工作,这里不阐述。假设你已经成功申请了企业账号,然后获取了CorpId和CorpSecret。这里我创建一个DDConfig类来保存这两个值.

2 编写DDHelper类

DDHelper类主要为了与钉钉API进行交互,实现Get和Post请求处理,代码如下:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using HttpRequest;
namespace myDDDev
{
public static class DDHelper
{
public static string GetAccessToken(HttpRequest.HttpHelper.HttpResult result)
{ if (result!=null)
{
StreamReader myStreamReader = new StreamReader(result.Result, System.Text.Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd(); M_AccessToken oat = Newtonsoft.Json.JsonConvert.DeserializeObject< M_AccessToken>(retString); if (oat!=null)
{
if(oat.errcode==)
{
return oat.access_token;
}
}
}
return "";
}
public static string GetAccessToken(string url)
{ if (url != "")
{
try
{
HttpRequest.HttpHelper.HttpResult result = HttpRequest.HttpHelper.Get(url);
M_AccessToken oat = Newtonsoft.Json.JsonConvert.DeserializeObject<M_AccessToken>(result.ToStringResult()); if (oat != null)
{
if (oat.errcode == )
{
return oat.access_token;
}
}
}
catch(Exception ex)
{
throw;
}
}
return "";
} public static string CreateDept(string url,string param)
{ if (url != "")
{
try
{
HttpRequest.HttpHelper.HttpResult result = HttpRequest.HttpHelper.Post(url, param, "application/json");
DDResult oat = Newtonsoft.Json.JsonConvert.DeserializeObject<DDResult>(result.ToStringResult()); if (oat != null)
{
if (oat.errcode == )
{
return "";
}
}
}
catch (Exception ex)
{
throw;
}
}
return "";
} }
//{"access_token":"","errcode":0,"errmsg":"ok"}
public class M_AccessToken
{
public string access_token { get; set; }
public int errcode { get; set; } public string errmsg { get; set; } }
public class DDResult
{ public int errcode { get; set; }
public string errmsg { get; set; } }
}

3 创建部门处理

在index.aspx.cs中编写创建部门的逻辑,代码如下:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using HttpRequest;
namespace myDDDev
{
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
//http://ddtalk.github.io/dingTalkDoc/
//获取AccessToken
//开发者在调用开放平台接口前需要通过CorpID和CorpSecret获取AccessToken。
//获取AccessToken的方法是向 https://oapi.dingtalk.com/gettoken?corpid=id&corpsecret=secrect GET请求。
string getUrl = string.Format("https://oapi.dingtalk.com/gettoken?corpid={0}&corpsecret={1}", DDConfig.__CorpID, DDConfig.__CorpSecret); //access_token 会失效,需要定期获取;
string access_token = DDHelper.GetAccessToken(getUrl); /*
开发者获取AccessToken后便可以调用开放平台其他接口。
以获取部门列表接口为例,获取部门列表接口为:
oapi.dingtalk.com/department/list
在请求该接口时,需要将获取的AccessToken作为请求参数拼装到URL中:
https://oapi.dingtalk.com/department/list?access_token=ACCESS_TOKEN
*/
//添加部门测试
string postUrl = string.Format("https://oapi.dingtalk.com/department/create?access_token={0}", access_token);
string param = "{\"access_token\":\"" + access_token + "\",\"name\":\"后勤部\",\"parentid\":\"1\",\"order\":\"3\",\"createDeptGroup\":\"false\"}";
//HttpRequest.HttpHelper.HttpResult result = HttpRequest.HttpHelper.Post(postUrl, param, "application/json");
//Response.Write(result.ToStringResult());
//code=0 表示创建从成功
string code = DDHelper.CreateDept(postUrl, param);
if(code=="")
{
Response.Write("创建成功");
}
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
}
}

4 手机验证

在手机上用钉钉客户端查看是否创建部门成功,如下图所示:

最新文章

  1. Windows下Python连接数据库(mysql, mongodb)
  2. CSS overflow 属性
  3. Eclipse学习笔记——快捷键
  4. 判断表字段是否存在default约束
  5. Android开发之应用程序窗体显示状态操作(requestWindowFeature()的应用)
  6. 三目运算符 改变&lt;a&gt;标签的class属性
  7. 解决PyGObject在pydev下报错的问题
  8. POJ 1146:ID Codes
  9. 解决ie 低版本的 background-size 兼容问题
  10. [Swift]LeetCode275. H指数 II | H-Index II
  11. [Java]异常在项目中的使用
  12. ELK 性能(4) — 大规模 Elasticsearch 集群性能的最佳实践
  13. Echart 仪表盘和柱形图
  14. c运行库、c标准库、windows API的区别和联系
  15. when coding in a fresh system
  16. css绘制进度条,持续转动的进度条
  17. linux centos 6.1 安装 redis
  18. 把KB转化为KB及以上单位
  19. css全局样式基础代码
  20. .Net Core Linux部署之进程守护 Supervisor 安装配置

热门文章

  1. Lagrange插值公式
  2. jsonp跨域问题
  3. Java中的逆变与协变
  4. JS魔法堂:ASI(自动分号插入机制)和前置分号
  5. php面向对象常见的专业术语
  6. js正则中的贪婪和非贪婪模式问题总结
  7. 兼容各浏览器的iframe - onlaod事件
  8. http服务器返回状态代码含义
  9. Sql Server中常见的数据类型
  10. MEF入门之不求甚解,但力求简单能讲明白(三)