///// 使用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> //[Obsolete("请使用TryCommonApi()方法")] //public static T Do<T>(Func<string, T> fun, string appId, string appSecret, bool retryIfFaild = true) // where T : WxJsonResult //{ // T result = null; // try // { // var accessToken = AccessTokenContainer.TryGetAccessToken(appId, appSecret, false); // result = fun(accessToken); // } // catch (ErrorJsonResultException ex) // { // if (retryIfFaild && ex.JsonResult.errcode == ReturnCode.获取access_token时AppSecret错误或者access_token无效) // { // //尝试重新验证 // var accessToken = AccessTokenContainer.TryGetAccessToken(appId, appSecret, true); // result = Do(fun, appId, appSecret, false); // } // } // return result; //} #endregion #endregion #region 异步方法 /// <summary> /// 【异步方法】使用AccessToken进行操作时,如果遇到AccessToken错误的情况,重新获取AccessToken一次,并重试。 /// 使用此方法之前必须使用AccessTokenContainer.Register(_appId, _appSecret);或JsApiTicketContainer.Register(_appId, _appSecret);方法对账号信息进行过注册,否则会出错。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="fun"></param> /// <param name="accessTokenOrAppId">AccessToken或AppId。如果为null,则自动取已经注册的第一个appId/appSecret来信息获取AccessToken。</param> /// <param name="retryIfFaild">请保留默认值true,不用输入。</param> /// <returns></returns> public static async Task <T> TryCommonApiAsync <T>(Func <string, Task <T> > fun, string accessTokenOrAppId = null, bool retryIfFaild = true) where T : WxJsonResult, new() { Func <Task <string> > accessTokenContainer_GetFirstOrDefaultAppIdAsyncFunc = async() => await AccessTokenContainer.GetFirstOrDefaultAppIdAsync(PlatformType.MP).ConfigureAwait(false); Func <string, Task <bool> > accessTokenContainer_CheckRegisteredAsyncFunc = async appId => await AccessTokenContainer.CheckRegisteredAsync(appId).ConfigureAwait(false); Func <string, bool, Task <IAccessTokenResult> > accessTokenContainer_GetAccessTokenResultAsyncFunc = (appId, getNewToken) => AccessTokenContainer.GetAccessTokenResultAsync(appId, getNewToken); int invalidCredentialValue = (int)ReturnCode.获取access_token时AppSecret错误或者access_token无效; var result = ApiHandlerWapperBase. TryCommonApiBaseAsync( PlatformType.MP, accessTokenContainer_GetFirstOrDefaultAppIdAsyncFunc, accessTokenContainer_CheckRegisteredAsyncFunc, accessTokenContainer_GetAccessTokenResultAsyncFunc, invalidCredentialValue, fun, accessTokenOrAppId, retryIfFaild); return(await result.ConfigureAwait(false)); }
/// <summary> /// 【异步方法】使用AccessToken进行操作时,如果遇到AccessToken错误的情况,重新获取AccessToken一次,并重试。 /// 使用此方法之前必须使用AccessTokenContainer.Register(_appId, _appSecret);或JsApiTicketContainer.Register(_appId, _appSecret);方法对账号信息进行过注册,否则会出错。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="fun"></param> /// <param name="accessTokenOrAppKey">AccessToken或AppKey。如果为null,则自动取已经注册的第一个corpId/corpSecret来信息获取AccessToken。</param> /// <param name="retryIfFaild">请保留默认值true,不用输入。</param> /// <returns></returns> public static async Task <T> TryCommonApiAsync <T>(Func <string, Task <T> > fun, string accessTokenOrAppKey, bool retryIfFaild = true) where T : WorkJsonResult { Func <Task <string> > accessTokenContainer_GetFirstOrDefaultAppIdAsyncFunc = async() => await AccessTokenContainer.GetFirstOrDefaultAppIdAsync(PlatformType.Work); Func <string, Task <bool> > accessTokenContainer_CheckRegisteredAsyncFunc = async appKey => { /* * 对于企业微信来说,AppId = key = CorpId+'@'+CorpSecret */ return(await AccessTokenContainer.CheckRegisteredAsync(appKey)); }; Func <string, bool, Task <IAccessTokenResult> > accessTokenContainer_GetAccessTokenResultAsyncFunc = (appKey, getNewToken) => { /* * 对于企业微信来说,AppId = key = CorpId+'@'+CorpSecret */ return(AccessTokenContainer.GetTokenResultAsync(appKey, getNewToken)); }; int invalidCredentialValue = (int)ReturnCode_Work.获取access_token时Secret错误_或者access_token无效; var result = ApiHandlerWapperBase. TryCommonApiBaseAsync( PlatformType.Work, accessTokenContainer_GetFirstOrDefaultAppIdAsyncFunc, accessTokenContainer_CheckRegisteredAsyncFunc, accessTokenContainer_GetAccessTokenResultAsyncFunc, invalidCredentialValue, fun, accessTokenOrAppKey, retryIfFaild); return(await result); }