/// <summary>
        /// 获取企业授权的access_token
        /// </summary>
        /// <param name="authCorpid">授权方corpid</param>
        /// <param name="permanentCode">永久授权码,通过GetPermanentCode获取</param>
        /// <returns>access_token	授权方(企业)access_token
        ///  expires_in 授权方(企业)access_token超时时间
        /// </returns>
        public CorpAccessToken GetCorpToken(string suiteAccessToken, string authCorpid, string permanentCode)
        {
            string postUrl  = "https://oapi.dingtalk.com/service/get_corp_token?suite_access_token=" + suiteAccessToken;
            string postData = "{\"auth_corpid\": \"" + authCorpid + "\",\"permanent_code\": \"" + permanentCode + "\"}";

            string          result          = WebRequestPost(postUrl, postData);
            var             ser             = new DataContractJsonSerializer(typeof(CorpAccessToken));
            var             ms              = new MemoryStream(Encoding.UTF8.GetBytes(result));
            CorpAccessToken corpAccessToken = (CorpAccessToken)ser.ReadObject(ms);

            ConfigurationManager.AppSettings["AccessToken"]        = corpAccessToken.access_token;
            ConfigurationManager.AppSettings["AccessTokenExpires"] =
                DateTime.Now.AddSeconds(corpAccessToken.expires_in).ToString("yyyy-MM-dd HH:mm:ss");

            return(corpAccessToken);
        }
Esempio n. 2
0
        public static string GetAccessToken()
        {
            string corpId = ConfigurationManager.AppSettings["CorpId"];//从配置文件获取corpId

            if (string.IsNullOrEmpty(corpId))
            {
                Helper.WriteLog(string.Format("CorpId 配置项没有配置!"));
            }
            string corpSecret = ConfigurationManager.AppSettings["CorpSecret"];//从配置文件获取CorpSecret

            if (string.IsNullOrEmpty(corpSecret))
            {
                Helper.WriteLog(string.Format("CorpSecret 配置项没有配置!"));
            }
            string accessTokenString = ConfigurationManager.AppSettings["AccessToken"];//从配置文件获取AccessToken

            Helper.WriteLog("AccessToken Cache:" + accessTokenString);
            string accessTokenExpires = ConfigurationManager.AppSettings["AccessTokenExpires"];//从配置文件获取AccessTokenExpires

            Helper.WriteLog("AccessToken Expires:" + accessTokenExpires);
            if (CheckTokenString(accessTokenString, accessTokenExpires))
            {
                Helper.WriteLog("AccessToken Cache:" + accessTokenString);
                return(accessTokenString);
            }
            string url = "https://oapi.dingtalk.com/gettoken?corpid={0}&corpsecret={1}";

            url = string.Format(url, corpId, corpSecret);
            string          token       = Helper.GetCorpExecuteResult(url);
            var             ser         = new DataContractJsonSerializer(typeof(CorpAccessToken));
            var             ms          = new MemoryStream(Encoding.UTF8.GetBytes(token));
            CorpAccessToken accessToken = (CorpAccessToken)ser.ReadObject(ms);

            ConfigurationManager.AppSettings["AccessToken"]        = accessToken.access_token;
            ConfigurationManager.AppSettings["AccessTokenExpires"] =
                DateTime.Now.AddSeconds(accessToken.expires_in).ToString("yyyy-MM-dd HH:mm:ss");

            Helper.WriteLog("AccessToken:" + accessToken.access_token);

            return(accessToken.access_token);
        }
Esempio n. 3
0
        /// <summary>
        /// 保存服务器推送临时授权码
        /// </summary>
        /// <param name="tb"></param>
        public void SaveTmpAuthCode(Hashtable tb)
        {
            SuiteKeyInfo    suiteKeyInfo = new SuiteKeyInfo(tb["SuiteKey"]);
            DingApiDispatch dingApi      = new DingApiDispatch(suiteKeyInfo.SuiteKey, suiteKeyInfo.SuiteSecret,
                                                               suiteKeyInfo.SuiteTicket);

            //用临时授权码获得永久授权码
            PermanentCode permanentCode = dingApi.GetPermanentCode(suiteKeyInfo.SuiteToken, tb["AuthCode"].ToString());

            SuiteCorpInfo suiteCorp = new SuiteCorpInfo(SuiteCorpInfo.Columns.Corpid,
                                                        permanentCode.auth_corp_info.corpid);

            suiteCorp.SuiteKey      = suiteKeyInfo.SuiteKey;
            suiteCorp.TmpAuthCode   = tb["AuthCode"].ToString();
            suiteCorp.PermanentCode = permanentCode.permanent_code;
            suiteCorp.Corpid        = permanentCode.auth_corp_info.corpid;
            suiteCorp.CorpName      = permanentCode.auth_corp_info.corp_name;

            //获取企业授权的access_token
            CorpAccessToken corpAccessToken = dingApi.GetCorpToken(suiteKeyInfo.SuiteToken, suiteCorp.Corpid,
                                                                   suiteCorp.PermanentCode);

            suiteCorp.AccessToken  = corpAccessToken.access_token;
            suiteCorp.TokenExpires = DateTime.Now.AddSeconds(corpAccessToken.expires_in);

            //获取企业授权的授权数据
            AuthCorp authCorp = dingApi.GetAuthInfo(suiteKeyInfo.SuiteKey, suiteKeyInfo.SuiteToken, suiteCorp.Corpid,
                                                    suiteCorp.PermanentCode);

            suiteCorp.CorpLogoUrl = authCorp.auth_corp_info.corp_logo_url;
            suiteCorp.Mobile      = authCorp.auth_user_info.mobile;
            suiteCorp.Name        = authCorp.auth_user_info.name;

            suiteCorp.Save();

            try
            {
                foreach (AgentInfo angInfo in authCorp.auth_info.agent)
                {
                    SuiteCorpAgent suiteCorpAgent = new Select().From(SuiteCorpAgent.Schema)
                                                    .Where(SuiteCorpAgent.Columns.CorpId).IsEqualTo(suiteCorp.Corpid)
                                                    .And(SuiteCorpAgent.Columns.AgentId).IsEqualTo(angInfo.agentid)
                                                    .ExecuteSingle <SuiteCorpAgent>() ?? new SuiteCorpAgent();
                    suiteCorpAgent.SuiteKey  = suiteKeyInfo.SuiteKey;
                    suiteCorpAgent.CorpId    = suiteCorp.Corpid;
                    suiteCorpAgent.AgentId   = angInfo.agentid;
                    suiteCorpAgent.AgentName = angInfo.agent_name;
                    suiteCorpAgent.LogoUrl   = angInfo.logo_url;
                    suiteCorpAgent.Appid     = angInfo.appid;

                    dingApi.ActivateSuite(suiteKeyInfo.SuiteKey, suiteKeyInfo.SuiteToken, suiteCorp.Corpid,
                                          suiteCorp.PermanentCode);
                    Agent agent = dingApi.GetAgent(suiteKeyInfo.SuiteKey, suiteKeyInfo.SuiteToken, suiteCorp.Corpid,
                                                   suiteCorp.PermanentCode, suiteCorpAgent.AgentId);
                    suiteCorpAgent.Description = agent.description;
                    suiteCorpAgent.IsClose     = agent.close;

                    suiteCorpAgent.Save();
                }
            }
            catch (Exception ex)
            {
                Helper.WriteLog("Err:" + ex.Message);
            }
        }