Пример #1
0
        internal static WebCredential GetCredential(string appId, string appSecret, string code)
        {
            WebCredential cred = null;

            if (creds.TryGetValue(appId, out cred))
            {
                if (cred.add_time.AddSeconds(cred.expires_in - 30) < DateTime.Now)
                {
                    //刷新
                    var rejson = Util.HttpGet2(string.Format(RefreshTokenUrl, appId, cred.refresh_token));
                    if (rejson.IndexOf("errcode") >= 0)
                    {
                        //42002	 refresh_token超时
                        cred       = new WebCredential();
                        cred.error = Util.JsonTo <ReturnCode>(rejson);
                    }
                    else
                    {
                        cred          = Util.JsonTo <WebCredential>(rejson);
                        cred.add_time = DateTime.Now;
                        creds[appId]  = cred;
                    }
                    return(cred);
                }
                else
                {
                    //
                    return(cred);
                }
            }
            else
            {
                //第一次
                var json = Util.HttpGet2(string.Format(TokenUrl, appId, appSecret, code));
                if (json.IndexOf("errcode") >= 0)
                {
                    cred       = new WebCredential();
                    cred.error = Util.JsonTo <ReturnCode>(json);
                }
                else
                {
                    cred          = Util.JsonTo <WebCredential>(json);
                    cred.add_time = DateTime.Now;
                    creds[appId]  = cred;
                }
                return(cred);
            }
        }
Пример #2
0
 internal static WebCredential GetCredential(string appId, string appSecret, string code)
 {
     WebCredential cred = null;
     if (creds.TryGetValue(appId, out cred))
     {
         if (cred.add_time.AddSeconds(cred.expires_in - 30) < DateTime.Now)
         {
             //刷新
             var rejson = Util.HttpGet2(string.Format(RefreshTokenUrl, appId, cred.refresh_token));
             if (rejson.IndexOf("errcode") >= 0)
             {
                 //42002	 refresh_token超时
                 cred = new WebCredential();
                 cred.error = Util.JsonTo<ReturnCode>(rejson);
             }
             else
             {
                 cred = Util.JsonTo<WebCredential>(rejson);
                 cred.add_time = DateTime.Now;
                 creds[appId] = cred;
             }
             return cred;
         }
         else
         {
             //
             return cred;
         }
     }
     else
     {
         //第一次
         var json = Util.HttpGet2(string.Format(TokenUrl, appId, appSecret, code));
         if (json.IndexOf("errcode") >= 0)
         {
             cred = new WebCredential();
             cred.error = Util.JsonTo<ReturnCode>(json);
         }
         else
         {
             cred = Util.JsonTo<WebCredential>(json);
             cred.add_time = DateTime.Now;
             creds[appId] = cred;
         }
         return cred;
     }
 }
Пример #3
0
        internal static WebCredential GetCredential(string appId, string appSecret, string code)
        {
            WebCredential cred = null;
            var           json = Util.HttpGet2(string.Format(TokenUrl, appId, appSecret, code));

            if (json.IndexOf("errcode") >= 0)
            {
                cred       = new WebCredential();
                cred.error = Util.JsonTo <ReturnCode>(json);
            }
            else
            {
                cred          = Util.JsonTo <WebCredential>(json);
                cred.add_time = DateTime.Now;
            }
            return(cred);
        }
Пример #4
0
        internal static WebCredential RefreshToken(string appId, string appSecret, string refresh_token)
        {
            WebCredential cred = null;
            //刷新
            var rejson = Util.HttpGet2(string.Format(RefreshTokenUrl, appId, refresh_token));

            if (rejson.IndexOf("errcode") >= 0)
            {
                //42002	 refresh_token超时
                cred       = new WebCredential();
                cred.error = Util.JsonTo <ReturnCode>(rejson);
            }
            else
            {
                cred          = Util.JsonTo <WebCredential>(rejson);
                cred.add_time = DateTime.Now;
            }
            return(cred);
        }
Пример #5
0
 /// <summary>
 /// 刷新网页授权access_token
 /// </summary>
 /// <param name="appId"></param>
 /// <param name="appSecret"></param>
 /// <param name="refresh_token"></param>
 /// <returns></returns>
 public static WebCredential RefreshWebAccessToken(string appId, string appSecret, string refresh_token)
 {
     return(WebCredential.RefreshToken(appId, appSecret, refresh_token));
 }
Пример #6
0
 /// <summary>
 /// 通过code换取网页授权access_token
 /// </summary>
 /// <param name="appId"></param>
 /// <param name="appSecret"></param>
 /// <param name="code"></param>
 /// <returns></returns>
 public static WebCredential GetWebAccessToken(string appId, string appSecret, string code)
 {
     return(WebCredential.GetCredential(appId, appSecret, code));
 }