码迷,mamicode.com
首页 > Windows程序 > 详细

C# 钉钉 钉盘文件管理

时间:2020-04-13 12:17:38      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:ref   name   agent   response   request   add   adsi   media   建议   

参考地址

1.获取AccessToken

https://ding-doc.dingtalk.com/doc#/serverapi2/eev437

private static string GetAccessToken()
        {
            DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
            OapiGettokenRequest request = new OapiGettokenRequest();
            request.Appkey = "Appkey";//填写对应的Appkey
            request.Appsecret = "Appsecret ";//填写对应的Appsecret 
            request.SetHttpMethod("GET");
            OapiGettokenResponse response = client.Execute(request);

            string result = response.AccessToken;
            return result;
        }

2.上传钉盘文件

单步上传文件,文件大小不得超过8M。

  /// <summary>
        /// 通过钉盘 上传文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string UploadFile(string filePath)
        {
            OapiFileUploadSingleRequest request = new OapiFileUploadSingleRequest();
            request.FileSize=(1000L);
            request.AgentId=("AgentId");//填写对应的AgentId
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/file/upload/single?" + WebUtils.BuildQuery(request.GetParameters()));
            // 必须重新new一个请求
            request = new OapiFileUploadSingleRequest();
            request.File=(new FileItem(filePath));
            OapiFileUploadSingleResponse response = client.Execute(request, Get_CorPsecret());
            return response.Body;//返回文件存储id
        }

3.发送文件给用户


        /// <summary>
        /// 通过钉盘里面的文件发送给用户
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="mediaID"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string SendUserMedia(string userID, string mediaID,string fileName)
        {
            OapiCspaceAddToSingleChatRequest request = new OapiCspaceAddToSingleChatRequest();
            request.AgentId= "AgentId";//填写对应的AgentId
            request.Userid=("Userid");//钉钉用户对应ID
            request.MediaId = HttpUtility.UrlEncodeUnicode(mediaID);
            request.FileName= fileName;
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/cspace/add_to_single_chat?" + WebUtils.BuildQuery(request.GetParameters()));
            OapiCspaceAddToSingleChatResponse response = client.Execute(request, Get_CorPsecret());
            return response.Body; 
        }

C# 钉钉 钉盘文件管理

标签:ref   name   agent   response   request   add   adsi   media   建议   

原文地址:https://www.cnblogs.com/Alex-Mercer/p/12690319.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!