<%
Class QqConnet
		'生成Session("State")数据.
	Public Function MakeRandNum()
		Randomize
		Dim width : width = 6 '随机数长度,默认6位
		width = 10 ^ (width - 1)
		MakeRandNum = Int((width*10 - width) * Rnd() + width)
	End Function

	Property Get APP_ID()    
        APP_ID = QQ_OAUTH_CONSUMER_KEY    
    End Property

	Private Function RequestUrl(url)
		Set XmlObj = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
		XmlObj.open "GET",url, false
		XmlObj.send
		RequestUrl = XmlObj.responseText
		Set XmlObj = nothing
	End Function
	'生成登录地址
	Public Function GetAuthorization_Code()
		Dim url, params
		url = "https://graph.qq.com/oauth2.0/authorize"
		params = "client_id=" & QQ_OAUTH_CONSUMER_KEY
		params = params & "&redirect_uri=" & QQ_CALLBACK_URL
		params = params & "&response_type=code"
		params = params & "&scope="&QQ_SCOPE
		params = params & "&state="&Session("State")
		url = url & "?" & params
		GetAuthorization_Code = (url)
	End Function

	Private Function CheckData(data,str)
		If Instr(data,str)>0 Then
		   CheckData = True
		Else
		   CheckData = False
		End If
	End Function
	
		'获取 access_token
	Public Function GetAccess_Token()
		Dim url, params,Temp
		Url="https://graph.qq.com/oauth2.0/token"
	    params = "client_id=" & QQ_OAUTH_CONSUMER_KEY
		params = params & "&client_secret=" & QQ_OAUTH_CONSUMER_SECRET
		params = params & "&redirect_uri=" & QQ_CALLBACK_URL
		params = params & "&grant_type=authorization_code"
		params = params & "&code="&Session("Code")
		params = params & "&state="&Session("State")
		url = Url & "?" & params
		Temp=RequestUrl(url)
		If CheckData(Temp,"access_token=") = True Then
           tokby7i24=CutStr(Temp,"access_token=","&")

'Response.Write "mmmmmmmmmmmmmmmmmmmmmmmm<BR>"&apid

		Application("fastlogin_temp_"&LCase(tokby7i24)) = Request.ServerVariables("SERVER_NAME")&":"&Request.ServerVariables("server_port")'------
		Application("fastlogin_temp_"&LCase(tokby7i24)&"_fasttime")=now'--------时间
'检查以前过期的变量,设置为nothing
FOR EACH apid IN Application.Contents
If InStr(apid,"_fasttime") Then
'Response.Write "<BR>"&apid
'Response.Write "<BR>"&Application(apid)
'Response.Write "<BR>"&DateDiff("s",Application(apid),NOW )
if DateDiff("s",Application(apid),NOW )>240 Then
Application.Contents.Remove(Replace(apid,"_fasttime",""))
Application.Contents.Remove(apid)
End if
End If
next
		Session("qq_oauth_token_secret") = tokby7i24
GetAccess_Token=tokby7i24

		Else
			Session("qq_oauth_token_secret") = ""
		   Response.Write("获取 access_token 时发生错误，错误代码："&CutStr(Temp,"{""error"":",",")) 
		   Response.End()
		End If
	End Function
	

	'检测是否合法登录！
	Public Function CheckLogin()
		Dim Code,mState
		Code=Trim(Request.QueryString("code"))
		'mState=Trim(Request.QueryString("state"))
		If Code<>"" Then
			CheckLogin = True
			Session("Code")=Code
		Else
			CheckLogin = False
		End If
	End Function

	'获取openid
	Public Function Getopenid()
		Dim url, params,Temp
		url = "https://graph.qq.com/oauth2.0/me"
		params = "access_token="&Session("Access_Token")
		url = Url & "?" & params
		Temp=RequestUrl(url)
		If Instr(Temp,"openid")>0 Then
		   Getopenid=CutStr(Temp,"openid"":""","""}")
		Else
		   Response.Write("获取 Openid 时发生错误，错误代码："&CutStr(Temp,"{""error"":",",")) 
		   Response.End()
		End If
	End Function

'获取用户信息,得到一个json格式的字符串
	Public Function GetUserInfo()
		Dim url, params, result
		url = "https://graph.qq.com/user/get_user_info"
		params = "oauth_consumer_key=" & QQ_OAUTH_CONSUMER_KEY
		params = params & "&access_token=" & Session("Access_Token")
		params = params & "&openid=" & Session("Openid")
		url = url & "?" & params
		GetUserInfo = RequestUrl(url)
		If CheckData(GetUserInfo,"nickname") = False Then
		   Response.Write("获取用户信息时发生错误，错误代码："&CutStr(GetUserInfo,"{""ret"":",",")) 
		   Response.End()
		End If
	End Function
	
	'获取腾讯微博登录用户的用户资料,得到一个json格式的字符串
	Public Function Get_Info()
		Dim url, params, result
		url = "https://graph.qq.com/user/get_info"
		params = "oauth_consumer_key=" & QQ_OAUTH_CONSUMER_KEY
		params = params & "&access_token=" & Session("Access_Token")
		params = params & "&openid=" & Session("Openid")
		params = params & "&format=json"
		url = url & "?" & params
		Get_Info = RequestUrl(url)
	End Function

	
	'获取用户名字,性别,从json字符串里截取相关字符
	Public Function GetUserName(json)
	    Dim nickname,sex
'		Response.write json&"<br>"
		nickname = CutStr(json, "nickname"": """,""",")
'		Response.write nickname&":000000000000000000000<br>"
		sex=CutStr(json, "gender"": ""","""")
	    GetUserName = Array(nickname,sex)
	End Function

		Public Function GetUserEmail(json)
	    Dim Email
		Email = Split(json, "email"": """)(1)
		Email = Split(Email, """,")(0)
	    GetUserEmail = Email
	End Function
	
	Public Function CutStr(data,s_str,e_str)
	    If Instr(data,s_str)>0 and Instr(data,e_str)>0 Then
		   CutStr = Split(data,s_str)(1)
		   CutStr = Split(CutStr,e_str)(0)
		Else
		   CutStr = ""
		End If
	End Function





	
	'获取临时token,返回一个数组Array(oauth_token, oauth_token_secret),用于生成调用地址
	Public Function GetTempToken()
		Dim url, params, result
		Dim temp
		result = Array("", "")
		url = "http://openapi.qzone.qq.com/oauth/qzoneoauth_request_token"
		params = "oauth_consumer_key=" & QQ_OAUTH_CONSUMER_KEY
		params = params & "&oauth_nonce=" & MakeRandNum()
		params = params & "&oauth_signature_method=HMAC-SHA1"
		params = params & "&oauth_timestamp=" & Timestamp()
		params = params & "&oauth_version=1.0"
		params = params & "&oauth_signature=" & MakeSign("GET", url, params, "")
		url = url & "?" & params
		temp = RequestUrl(url)
		temp = Split(temp, "&")
		result(0) = Split(temp(0), "=")(1)
		result(1) = Split(temp(1), "=")(1)
		Application("fastlogin_temp_"&LCase(result(0))) = Request.ServerVariables("SERVER_NAME")&":"&Request.ServerVariables("server_port")'------
		Application("fastlogin_temp_"&LCase(result(0))&"_fasttime")=now'--------时间
'检查以前过期的变量,设置为nothing
FOR EACH apid IN Application.Contents
If InStr(apid,"_fasttime") Then
'Response.Write "<BR>"&apid
'Response.Write "<BR>"&Application(apid)
'Response.Write "<BR>"&DateDiff("s",Application(apid),NOW )
if DateDiff("s",Application(apid),NOW )>240 Then
Application.Contents.Remove(Replace(apid,"_fasttime",""))
Application.Contents.Remove(apid)
End if
End if
NEXT

		Session("qq_oauth_token_secret") = result(1)
		GetTempToken = result
	End Function

	

End Class

%>