<!--#include file="../../MainClass.asp"-->
<!--#include file="../inc.asp"-->
<%
dim rArr,Fun:rArr=requestArrey("",0)
Set Fun=server.CreateObject("inc_inc_MainClass.CommonFun")
Select case rArr(0)
 	case else:qq_callback():get_openid()
End Select
function qq_callback()
  dim xmlobj,appid,callback,appkey
  SET xmlobj=server.CreateObject("inc_inc_MainClass.xml")
  xmlobj.load "../api.config","xmlfile"
  appid=xmlobj.getAttributes("rs:data/z:row","api_qqappid",0)
  appkey=xmlobj.getAttributes("rs:data/z:row","api_qqappkey",0)
  callback=xmlobj.getAttributes("rs:data/z:row","api_qqcallback",0)
  Set xmlobj=Nothing
  ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  if(lcase(Request("state")) = lcase(Session("state"))) Then
	Dim token_url,result
	token_url = "https://graph.qq.com/oauth2.0/token"
	result = getRemoteContent(token_url,"grant_type=authorization_code&client_id="&appid&"&redirect_uri="&server.URLEncode(callback) & "&client_secret="&appkey &"&code="&REQUEST("code"),"text","get")
	if instr(result,"callback")<>0 then
		dim lpos:lpos = instr(result, "(")
		dim rpos:rpos = instr(result, ")")
		result  = mid(result, lpos + 1, rpos - lpos -1)
		dim obj:set obj = getjson(result)
		if isobject(obj) Then
		  conn.echo "<h3>error:</h3>" & obj.error
		  conn.echo "<h3>msg:</h3>" & obj.error_description
		End If
		set obj=nothing
		conn.die ""
	end if
	if result<>"" then
		dim arr:set arr=parse_str(result)
		conn.wCookie "access_token", arr("access_token")
		conn.wCookie "qqappid", appid
	else
	  conn.die "error!"
	end if
  Else 
	conn.Echo "The state does not match. You may be a victim of CSRF."
  End If
End Function
Function requestArrey(Byval url,Byval ArrI)'返回URl以"-"组成的数组.(1)声名数组的大小
  dim paras,i,num,parasArr,urlnum
  if url="" then url=request.QueryString
  urlnum=InstrRev(url,fileSuffix)
  if urlnum<>0 then paras=left(url,urlnum-1) else paras=url
  parasArr=split(paras,"/")
  num=ubound(parasArr)
  redim parasArray(ArrI)
  for i=0 to ArrI
	if i<=num then
	  parasArray(i)=parasArr(i)
	End if
  next
  requestArrey=parasArray
End Function
function get_openid()
  dim graph_url:graph_url = "https://graph.qq.com/oauth2.0/me"
  dim result:result=getRemoteContent(graph_url,"access_token="&conn.rCookie("access_token"),"text","get")
  if instr(result,"callback")<>0 then
	dim lpos:lpos = instr(result, "(")
	dim rpos:rpos = instr(result, ")")
	result  = mid(result, lpos + 1, rpos - lpos -1)
	dim obj:set obj = getjson(result)
	if isobject(obj) Then
	  conn.wCookie "openid", obj.openid
	End If
	set obj=nothing
  end if
End Function
if fun.isnul(conn.rCookie("openid")) then 
  conn.echo "没有返回openid!"
else
  response.write "<div style='margin-top:90px;color:#666;font-size:16px;text-align:center;'>正在登录中，请稍候！！！如果长时间没有反应请<a href=""index.asp?qqbind"" target=""parent"" style='color:red'>点此跳转</a>。</div>"
  response.Redirect"index.asp?qqbind"
end if
%>
<script language="javascript" type="text/javascript" runat="server">
function getjson(str){
  try{eval("var jsonStr=("+str+")");}
  catch(ex){var jsonStr=null;}
  return jsonStr;
}
</script>
