业务系统——钉钉调用接口上传文件报错:Could not find token at index 0

  1. 使用的是旧版的SDK

        /// <summary>
        /// 获取审批钉盘信息
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static string GetSpaceId(string userId, string token)
        {
            DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/cspace/info");
            OapiProcessinstanceCspaceInfoRequest req = new OapiProcessinstanceCspaceInfoRequest();
            req.UserId = userId;
            OapiProcessinstanceCspaceInfoResponse rsp = client.Execute(req, token);
            if (rsp.Errcode == 0 && rsp.Errmsg == "ok")
            {
                return rsp.Result.SpaceId.ToString();
            }
            else
            {
                throw new Exception("钉钉Token接口返回异常:" + rsp.Body);
            }
        }


        /// <summary>
        /// 上传本地文件钉盘
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        /// 参考:https://open.dingtalk.com/document/orgapp/single-step-file-upload
        public static string UploadFile(string token)
        {
            OapiFileUploadSingleRequest request = new OapiFileUploadSingleRequest();
            request.FileSize = 7;
            request.AgentId = new DDkeyModle().AgentId.ToString();
            request.AddOtherParameter("access_token", token); 
            DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/file/upload/single?" + WebUtils.BuildQuery(request.GetParameters()));
            // 必须重新new一个请求
            request = new OapiFileUploadSingleRequest();
            request.File = new FileItem("D:\\Desktop\\2.txt");
            OapiFileUploadSingleResponse rsp = client.Execute(request, token);
            if (rsp.Errcode == 0 && rsp.Errmsg == "ok")
            {
                return rsp.MediaId;
            }
            else
            {
                throw new Exception("钉钉Token接口返回异常:" + rsp.Body);
            }
        }

  1. 上传文件报错:Could not find token at index 0

根据原因是是SDK解析接口返回的body没有解析到Token,

我查看了返回的body,其中有说明:

var strShow = "According to the access control policy, you are not allowed to access this website. If you have any doubt, please contact the network administrator.";
根据访问控制策略,您目前无法访问该网站。如有疑问,请与网络管理员联系。

原因就是公司的上网安全策略的原因,不允许上传文件到钉钉,over!

posted @ 2024-09-28 12:11  shanzm  阅读(9)  评论(0编辑  收藏  举报
TOP