小程序的accesstoken保存两个小时

创建一个保存accesstoken的表,内有accesstoken(nchar),timee(datetime)两字段

public static string Getaccestoken()//获取可用的accesstoken
    {
        string f2=null;
        SqlConnection myconnection = new SqlConnection(ConfigurationManager.AppSettings["tosql"]);
        SqlDataAdapter sda = new SqlDataAdapter("select * from 保存accesstoken的表", myconnection);
        //myconnection.Open();
        DataSet ds = new DataSet();
        sda.Fill(ds);
        DateTime timee =(DateTime)ds.Tables[0].Rows[0]["timee"];
        DateTime n = DateTime.Now;
        double s2 = (n - timee).TotalHours;
        //myconnection.Close();
        ds.Dispose();
        if (s2 >= 1.9)
        {   
            string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的appid&secret=你的secret";
            f2= Get(url);
            string[] step2 = f2.Split('"');
            string acctoken = step2[3];//所需的accesstokne
            SqlCommand addtoken = new SqlCommand("update 保存accesstoken的表 set accesstoken='" + acctoken + "',timee='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")+"'",myconnection);
            myconnection.Open();
            addtoken.ExecuteNonQuery();
            myconnection.Close();
            return acctoken;
        }
        else
        {
            string acctoken = (string)ds.Tables[0].Rows[0]["accesstoken"];
            return acctoken;
        }

    }
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值