获取钉钉token

ALTER PROCEDURE [dbo].[sp_getdingtalk](@platform_key INT,@token varchar(100) OUTPUT)
AS
BEGIN 
 IF EXISTS(SELECT 1 FROM y_platform_token WHERE platform_key=@platform_key AND DATEDIFF(SECOND,create_time,GETDATE())<7200)
 BEGIN
  SELECT @token=token FROM y_platform_token WHERE platform_key=@platform_key 
  RETURN 0
 END 
  
 
 DECLARE @corpid VARCHAR(100)
 DECLARE @corpsecret varchar(100)
 DECLARE @obj Int
 Declare @url Varchar(400)
 Declare @response Varchar(8000)
 
 SELECT @corpid=corpid,@corpsecret=corpsecret FROM dbo.y_platform WHERE platform_key=@platform_key
 Set @url='https://oapi.dingtalk.com/gettoken?corpid='+@corpid+'&corpsecret='+@corpsecret
 Exec sp_OACreate 'Msxml2.ServerXMLHTTP.3.0',@obj Out
 Exec sp_OAMethod @obj,'Open',null,'GET',@url,false
 Exec sp_OAMethod @obj,'Send'
 Exec sp_OAGetProperty @obj,'responseText',@response Out
 Exec sp_OADestroy @obj
 
 SELECT @token=StringValue FROM dbo.parseJSON(@response) WHERE NAME='access_token'
 IF ISNULL(@token,'')=''
 BEGIN
  RETURN -1
 END  
 
 UPDATE y_platform_token SET token=@token,create_time=GETDATE() WHERE platform_key=@platform_key
 IF @@ROWCOUNT=0
 BEGIN
  INSERT INTO y_platform_token(platform_key,token,create_time) VALUES(@platform_key,@token,GETDATE())
 END 
 
 RETURN 0  
END 

 

你可能感兴趣的:(SQL,Server)