为避免重复调用接口获取Access_Token,建立数据表存储获取的Access_Token,并记录过期时间。当在有效期内再次调用时,直接返回数据表中的Access_Token。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
create PROC P_get_access_token ( @secret as Varchar(512), @access_token VARCHAR(512) OUTPUT ) AS select top 1 @access_token=access_token from token where [secret]=@secret and expirestime>GETDATE() and createtime<GETDATE() if @access_token is null begin declare @corpid as Varchar(200),@url as Varchar(1000) declare @expirestime datetime,@PostData Varchar(8000),@ResponseText as Varchar(8000) --delete from token where expirestime<GETDATE() select @corpid=setvalue from [init] where setname='corpid' select @url=setvalue from [init] where setname='url' and settype='access_token' set @url=replace(replace(@url,'[secret]',@secret),'[corpid]',@corpid) --获取@access_token exec P_Url_SendRequest @url ,@PostData,@ResponseText OUTPUT select @access_token=stringvalue from parsejson(@ResponseText) where NAME='access_token' ---parsejson是JSON解析函数 select @expirestime=dateadd(s,cast(stringvalue as int)-600,getdate()) from parsejson(@ResponseText) where NAME='expires_in' ---parsejson是JSON解析函数 insert into token([secret],access_token,expirestime,createtime) values (@secret,@access_token,@expirestime,getdate()) end --select * from token GO |
© 2017, ITJOY.NET. 版权所有. 如未注明,均为原创,转载请注明出处。
“MSSQL 获取企业微信Access_Token-P_Get_Access_Token”的一个回复