微信开发 - 获取网页授权access_token、openid以及用户信息

微信开发 - 获取网页授权access_token、openid以及用户信息

 

 

微信公众平台开发中,会遇到一个叫openid的东西,它是公众号普通用户的一个唯一标识,即同一用户针对同一公众号的openid是唯一的。

 

在关注者与公众号产生消息交互后,公众号可获得关注者的openid,但请注意,在未关注公众号时,用户访问公众号的网页,也会产生一个用户和公众号唯一的openid。

 

 

获取网页授权access_token、openid

 

一、用户同意授权,获取code

 

https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect

1、appid 公众号的唯一标识

2、redirect_uri 授权后重定向的回调链接地址,请使用urlencode对链接进行处理

3、response_type 返回类型,请填写code

4、scope 应用授权作用域

  (1)snsapi_base:不弹出授权页面,直接跳转,只能获取用户openid

  (2)snsapi_userinfo:弹出授权页面,可通过openid拿到昵称、性别、所在地。

5、state 重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节

6、#wechat_redirect 无论直接打开还是做页面302重定向时候,必须带此参数

注意这个接口中有个参数scope 默认有2个值snsapi_base和snsapi_userinfo,这个接口会根据scope 来生成不同的code并且获取不同作用的access_token,不管scope传什么值都能在得到对应access_token的同时得到openid, 如果你只需要得到opendid 那使用snsapi_base参数到此结束了,如果需要获取用户的其他信息比如昵称、地址、就要用snsapi_userinfo 会弹出授权。

snsapi_base为例:以下链接可设置为微信公众平台"自定义菜单"的页面地址,点击后会发送请求到redirect_uri,并携带参数code。

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe49d******43c1cd&redirect_uri=http%3A%2F%2FdomainName%2FserverName%2FcontrollerName%2FactionName

&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect

 

二、通过code换取网页授权access_token及openid

 

def getOpenId(String code)
{
    def CODE = code
    def APPID = "wxe49d******43c1cd"
    def SECRET = "217c05ff85************db8f4c9371"

    URL url = new URL("https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code")
    def params = "APPID=" + URLEncoder.encode(APPID, "UTF-8") + 
                 "&SECRET=" + URLEncoder.encode(SECRET, "UTF-8") + 
                 "&CODE=" + URLEncoder.encode(CODE, "UTF-8")

    HttpURLConnection connection = (HttpURLConnection) url.openConnection()
    connection.setDoOutput(true)
    connection.setRequestMethod("POST")
    connection.outputStream.withWriter { Writer writer -> writer.write params }
    def response = connection.inputStream.withReader { Reader reader -> reader.text }
    def openId = JSON.parse(response).getAt("openid")
    return openId
}

正确时返回的JSON数据包如下:
{
   "access_token":"ACCESS_TOKEN",
   "expires_in":7200,
   "refresh_token":"REFRESH_TOKEN",
   "openid":"OPENID",
   "scope":"SCOPE"
}

 注意:其中,APPID、SECRET可在微信公众平台中获取。 

 

获取用户信息

 

https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN

这个接口中的access_token是获取code的时候scope 参数为snsapi_userinfo时换取的网页授权access_token

微信还有一个获取用户基本信息的接口,但是这个接口需要你关注了公众号。 

https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN




此接口的access_token 是接口基础调用access_token 而不是网页授权access_token。在用户和公众号产生消息交互或关注后事件推送后,才能根据用户OpenID来获取用户基本信息。这个接口,包括其他微信接口,都是需要该用户(即openid)关注了公众号后,才能调用成功的。

 

批量获取用户信息

 

开发者可通过该接口来批量获取用户基本信息。最多支持一次拉取100条。

 

https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=ACCESS_TOKEN





 此接口的access_token 同样也是接口基础调用的access_token。

猜你喜欢

转载自hellolove.iteye.com/blog/2329236