-

微信公众号授权登录

微信公众号授权登录的使用最为常见,当然只是一些会只有登录,所以选择点击授权登
第一步:用户同意授权,获取code

确保公众账号拥有授权作用域(scope参数)的权限的前提下,微信公众号 》 网页授权域名 》 有对应的网址 如 xiyueta.com 》 在设置的时候把 强制使用 https 去掉,就可以同时使用http方式


https://open.weixin.qq.com/connect/oauth2/authorize
?appid=APPID // 公众号的唯一标识
&redirect_uri=REDIRECT_URI // 授权后重定向的回调链接地址, 使用 urlEncode 对链接进行处理
&response_type=code // 返回类型,填写code
&scope=SCOPE // 应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且, 即使在未关注的情况下,只要用户授权,也能获取其信息 )
&state=STATE#wechat_redirect // 重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节。#wechat_redirect    是   无论直接打开还是做页面302重定向时候,必须带此参数

若提示“该链接无法访问”,请检查参数是否填写错误,是否拥有scope参数对应的授权作用域权限。

            
第二步:通过code换取网页授权access_token

公众号可通过下述接口来获取网页授权access_token。如果网页授权的作用域为snsapi_base,则本步骤中获取到网页授权access_token的同时,也获取到了openid,snsapi_base式的网页授权流程即到此为止。


// 请求方法
// 获取code后,请求以下链接获取access_token: 
https://api.weixin.qq.com/sns/oauth2/access_token
?appid=APPID // 公众号的唯一标识
&secret=SECRET // 公众号的appsecret
&code=CODE // 填写第一步获取的code参数
&grant_type=authorization_code //   填写为authorization_code

            
第三步:刷新access_token

access_token拥有较短的有效期,当access_token超时后,可以使用refresh_token进行刷新,refresh_token有效期为30天,当refresh_token失效之后,需要用户重新授权。


// 请求方法
// 获取第二步的refresh_token后,请求以下链接获取access_token: 
https://api.weixin.qq.com/sns/oauth2/refresh_token
?appid=APPID // 公众号的唯一标识
&grant_type=refresh_token // 填写为refresh_token
&refresh_token=REFRESH_TOKEN // 填写通过access_token获取到的refresh_token参数

            
第四步:拉取用户信息(需scope为 snsapi_userinfo)

如果网页授权作用域为snsapi_userinfo,则此时开发者可以通过access_token和openid拉取用户信息了。


//请求方法
//http:GET(请使用https协议)
https://api.weixin.qq.com/sns/userinfo
?access_token=ACCESS_TOKEN // 网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同
&openid=OPENID // 用户的唯一标识
&lang=zh_CN // 国家地区语言版本

            
案例代码

一个测试成功的案例代码


<%
'跳转到服务器指定网址'
dim url,canshu
url="https://open.weixin.qq.com/connect/oauth2/authorize"
canshu=canshu & "?appid=XXX"
canshu=canshu & "&redirect_uri=" & server.URLEncode("http://xiyueta.com/wxlogin.asp")
canshu=canshu & "&response_type=code"
canshu=canshu & "&scope=snsapi_userinfo"
canshu=canshu & "&state=123#wechat_redirect"


' &scope=SCOPE // 应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且, 即使在未关注的情况下,只要用户授权,也能获取其信息 )

 

url = url &canshu 

response.redirect(url) 
%>

            

验证成功并写入数据库

 
<!--#Include file = "../inc/config.asp"--><%

call loadmin()
sub loadmin()
    dim isDebug  '调用'
    isDebug=false

    if isDebug then
        call echo("code",request("code"))
        call echo("state",request("state"))
        doevents
    end if

dim url,canshu,s,refresh_token,access_token,useropenid
url="https://api.weixin.qq.com/sns/oauth2/access_token"
canshu="?appid=XXX"
canshu=canshu & "&secret=6f209e4695c7f927e2937123f7155870"
canshu=canshu & "&code=" &  request("code")
canshu=canshu & "&scope=snsapi_userinfo"
canshu=canshu & "&grant_type=authorization_code"

s=gethttpurl(url &canshu,"utf-8")
if isDebug then call echo("s",s)
refresh_token=getStrCut(s,"""refresh_token"":""",""",",0)
if isDebug then call echo("取refresh_token",refresh_token)


'取access_token
url="https://api.weixin.qq.com/sns/oauth2/refresh_token"
canshu="?appid=XXX" 
canshu=canshu & "&refresh_token=" & refresh_token  '填写通过access_token获取到的refresh_token参数'
canshu=canshu & "&grant_type=refresh_token&"  '填写为refresh_token'
s=gethttpurl(url &canshu,"utf-8")
if isDebug then call echo("url",url & canshu)
if isDebug then call echo("s",s)

 
access_token=getStrCut(s,"""access_token"":""",""",",0)
if isDebug then call echo("access_token",access_token)
useropenid=getStrCut(s,"""openid"":""",""",",0)
if isDebug then call echo("取useropenid",useropenid)

if isDebug then call hr()
url="https://api.weixin.qq.com/sns/userinfo"
canshu="?access_token="  & access_token  '网页授权接口调用凭证'
canshu=canshu & "&openid=" & useropenid
canshu=canshu & "&lang=zh_CN&"  '填写为refresh_token'
 



s=gethttpurl(url &canshu,"utf-8")
if isDebug then call echo("url",url & canshu)
if isDebug then call echo("s",s)

dim nickname,sex,province,city,headimgurl
nickname=getStrCut(s,"""nickname"":""",""",",0)   '昵称'
sex=getStrCut(s,",""sex"":",",",0)   '性别'
province=getStrCut(s,"""province"":""",""",",0)   '省'
city=getStrCut(s,"""city"":""",""",",0)   '城市'
headimgurl=getStrCut(s,"""headimgurl"":""",""",",0)   '头像'
headimgurl=replace(headimgurl,"\/","/")

if isDebug then 
call echo("nickname",nickname)
call echo("sex",sex)
call echo("province",province)
call echo("city",city)
call echo("headimgurl",headimgurl & "")
end if


call openconn()
    dim sql
    sql="select * from " & db_PREFIX & "member Where wxopenid='"& useropenid &"'"
    rs.open sql ,conn,1,3
    if rs.eof then  
        rs.addnew
        rs("username")="wx" & getrnd(8)  '用户名'
    end if  
    rs("wxopenid")=useropenid
    rs("pic")=headimgurl
    rs("nickname")=nickname
    rs("province")=province
    rs("city")=city
    rs("sex")=IIF(sex="0","男","女")      
    rs.update:rs.close
    
    rs.open sql,conn,1,1    
    if not rs.eof then
        session("memberid")=rs("id")
    end if
    if isDebug then call echo("提示","登录成功")
 
    response.redirect("/")

   if isDebug then  call die("ss")

end sub
 
%>