微信OAuth授权获取用户OpenId-JAVA(个人经验)


http://mp.weixin.qq.com/wiki这个是官网的接口文档

微信授权获取用户openid-JAVA 

开发微信测试需要用到的代码和jar包都在里面  包括核心代码  上面图片可以扫一扫 你懂得

链接: http://mobile.qzone.qq.com/l?g=1357&sharekey=e43cb6432868c4b709c5351c3c82d3de

/**
     * 网页授权获取openId第2步,根据code取得openId
     *
     * @param appid   公众号的唯一标识
     * @param secret    公众号的appsecret密钥
     * @param code    code为换取access_token的票据          
     * @return
     */

代码为OAuthInfo 不在此博客中 OAuthInfo只获取到Openid 。获取详细信息。继续调用微信接口

拉取用户信息(需scope为 snsapi_userinfo)

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

请求方法

http:GET(请使用https协议) https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN

 

第一步:用户同意授权,获取code

在确保微信公众账号拥有授权作用域(scope参数)的权限的前提下(服务号获得高级接口后,默认带有scope参数中的snsapi_base和snsapi_userinfo),引导关注者打开如下页面:

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

我的代码如下:GetWeiXinCode

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package  cn.elve.jxt.weixin.weixin.util;
 
import  java.net.URLEncoder;
import  cn.elve.jxt.weixin.util.Constants;
/**
  * 获取微信的code
  * @author 宗潇帅
  * @修改日期 2014-7-21下午1:01:45
  */
public  class  GetWeiXinCode {
     public  static  String  GetCodeRequest =  "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect" ;
     public  static  String getCodeRequest(){
         String result =  null ;
         GetCodeRequest  = GetCodeRequest.replace( "APPID" , urlEnodeUTF8(Constants.appId));
         GetCodeRequest  = GetCodeRequest.replace( "REDIRECT_URI" ,urlEnodeUTF8(Constants.REDIRECT_URI));
         GetCodeRequest = GetCodeRequest.replace( "SCOPE" , Constants.SCOPE);
         result = GetCodeRequest;
         return  result;
     }
     public  static  String urlEnodeUTF8(String str){
         String result = str;
         try  {
             result = URLEncoder.encode(str, "UTF-8" );
         catch  (Exception e) {
             e.printStackTrace();
         }
         return  result;
     }
     public  static  void  main(String[] args) {
         System.out.println(getCodeRequest());
     }
}

替换相应的APPID APPSECRET SCOPE

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

获取code后,请求以下链接获取access_token: 
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code

具体做法与上面基本一致。更换相对应的值。需要注意的是code可以写一个Servlet获取。String code = request.getParameter("code");get/post都可以。

这样子就会返回一下json格式数据

{
   "access_token":"ACCESS_TOKEN",
   "expires_in":7200,
   "refresh_token":"REFRESH_TOKEN",
   "openid":"OPENID",
   "scope":"SCOPE"
}

我们需要对这个json格式数据进行转换。大写自己都是用自己账号获取的数据。所有做了替换。

                oAuthInfo = new OAuthInfo();网页授权接口调用凭证5个参数

                 oAuthInfo.setAccessToken(jsonObject.getString("access_token"));
                oAuthInfo.setExpiresIn(jsonObject.getInt("expires_in"));
                oAuthInfo.setRefreshToken(jsonObject.getString("refresh_token"));
                oAuthInfo.setOpenId(jsonObject.getString("openid"));
                oAuthInfo.setScope(jsonObject.getString("scope"));

根据json。获取key,就能得到value。
这就获取到用户的openid。应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息)我自己用的作用域为snsapi_userinfo。用户点击跳转页面为

写一个Servlet专门接收传递过来的code。进行相应的操作。

1.OAuthServlet 对code进行access——token的验证

2.GetWeiXinCode的方法调用接口地址。得到相应code。

3.OAuthInfo 返回数据相应的参数的PO类。set/get方法

4.WeiXinUtil添加一个方法 publicOAuth  getOAuthInfo(String appid, String secret, String code)得到json格式。并使用JSONObject读取出自己想要的数据。

 

个人微博 http://weibo.com/u/2205636212 

个人博客 http://my.oschina.net/xshuai/blog 

公开QQ  783021975

个人联盟 http://www.bengbeng.com/?sid=687095   


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM