TryGetToken() public static method

使用完整的应用凭证获取Token,如果不存在将自动注册
public static TryGetToken ( string corpId, string corpSecret, bool getNewToken = false ) : string
corpId string
corpSecret string
getNewToken bool
return string
Esempio n. 1
0
        /// <summary>
        /// 获取调用微信JS接口的临时票据
        /// </summary>
        /// <param name="corpId"></param>
        /// <param name="corpSecret"></param>
        /// <returns></returns>
        public static JsApiTicketResult GetTicket(string corpId, string corpSecret)
        {
            var accessToken = AccessTokenContainer.TryGetToken(corpId, corpSecret);
            //var accessToken = GetToken(corpId, corpSecret).access_token;

            var url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token={0}",
                                    accessToken.AsUrlData());

            JsApiTicketResult result = Get.GetJson <JsApiTicketResult>(url);

            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// 使用AccessToken进行操作时,如果遇到AccessToken错误的情况,重新获取AccessToken一次,并重试
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="appId"></param>
        /// <param name="appSecret"></param>
        /// <param name="fun">第一个参数为accessToken</param>
        /// <param name="retryIfFaild"></param>
        /// <returns></returns>
        public static T Do <T>(string appId, string appSecret, Func <string, T> fun, bool retryIfFaild = true) where T : WxJsonResult
        {
            T result = null;

            try
            {
                var accessToken = AccessTokenContainer.TryGetToken(appId, appSecret, false);
                result = fun(accessToken);
            }
            catch (ErrorJsonResultException ex)
            {
                if (retryIfFaild && ex.JsonResult.errcode == ReturnCode.验证失败)
                {
                    //尝试重新验证
                    var accessToken = AccessTokenContainer.TryGetToken(appId, appSecret, true);
                    result = Do <T>(appId, appSecret, fun, false);
                }
            }
            return(result);
        }