服务器获取微信用户信息,微信开发之三: OAuth2.0授权(获取用户信息)

微信公众平台OAuth2.0授权详细步骤如下:

1. 用户关注微信公众账号。

2. 微信公众账号提供用户请求授权页面URL。

3. 用户点击授权页面URL,将向服务器发起请求

4. 服务器询问用户是否同意授权给微信公众账号(scope为snsapi_base时无此步骤)

5. 用户同意(scope为snsapi_base时无此步骤)

6. 服务器将CODE通过回调传给微信公众账号

7. 微信公众账号获得CODE

8. 微信公众账号通过CODE向服务器请求Access Token

9. 服务器返回Access Token和OpenID给微信公众账号

10. 微信公众账号通过Access Token向服务器请求用户信息(scope为snsapi_base时无此步骤)

11. 服务器将用户信息回送给微信公众账号(scope为snsapi_base时无此步骤)

总体 来说分为如下步骤:

1.引导用户进入授权页面-----获取code

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";

@RequestMapping(value="/toAutho",method=RequestMethod.GET)

@ResponseBody

public String getCodeRequest(){

GetCodeRequest  = GetCodeRequest.replace("APPID", urlEnodeUTF8(AuthUtil.APPID))

.replace("REDIRECT_URI",urlEnodeUTF8(WxPayConfig.REDIRCT_URL))

.replace("SCOPE", "snsapi_userinfo");

return GetCodeRequest;

}

public static String urlEnodeUTF8(String str){

String result = str;

try {

result = URLEncoder.encode(str,"UTF-8");

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

注意:REDIRECT_URI回调路径,必须是公众号公回调域名下,当用户同意授权后,微信服务器会将授权的code传递到REDIRCT_URL  所表示的链接地址

public static final String REDIRCT_URL  = "http://157f1604y1.51mypc.cn/ssm02/login/toWxCode";回调路径访问下面的接口,并携带code,每次请求code都不同

2.微信同意授权后回调,携带code------获取access_token

@RequestMapping(value="/toWxCode",method=RequestMethod.GET)

public void userInfo(String code){

//3.获取openId,access_token并存放在redis里边,为后边成功开通后调用微信消息模板提醒使用

String url=WxPayConfig.OPEN_ID+"appid="+AuthUtil.APPID+"&secret="+AuthUtil.APPIDSECRET

+"&code="+code+"&grant_type=authorization_code";

String weixinPost = HttpXmlUtils.httpsRequest(url, WxPayConfig.METHOD, null).toString();

JSONObject jsonParam = JSONObject.parseObject(weixinPost);

String openId=jsonParam.getString("openid");

String access_token =jsonParam.getString("access_token");

//4.获取用户信息:unionid

String user_url=WxPayConfig.USER_URL+"access_token="+access_token+"&openid="+openId

+"&lang=zh_CN";

String userInfo = HttpXmlUtils.httpsRequest(user_url, WxPayConfig.METHOD_GET, null).toString();

JSONObject jsonUser = JSONObject.parseObject(userInfo);

String unionid=jsonUser.getString("unionid");

String nickname=jsonUser.getString("nickname");//昵称

System.out.println("同意授权后的code"+"================="+code);

System.out.println("access_token"+"================="+access_token);

System.out.println("openId"+openId+"============昵称"+nickname);

}

//public static final String OPEN_ID = "https://api.weixin.qq.com/sns/oauth2/access_token?";

//public static final String USER_URL = "https://api.weixin.qq.com/sns/userinfo?";

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue项目中使用微信OAuth2.0授权登录,可以通过以下步骤实现: 1. 在微信公众平台或开放平台中申请应用,并获取到appID和appSecret。 2. 在Vue项目中安装wechat-oauth模块,该模块提供了微信OAuth2.0的相关接口。 ``` npm install wechat-oauth ``` 3. 在Vue项目的后端服务器中,编写一个处理微信OAuth2.0授权登录的回调接口,并在该接口中调用wechat-oauth模块提供的接口,实现用户授权登录。 ``` const OAuth = require('wechat-oauth'); const client = new OAuth(appId, appSecret); // 获取授权地址并重定向到该地址 router.get('/wechat/login', async (ctx, next) => { const redirectUrl = client.getAuthorizeURL( 'http://your-redirect-url', '', 'snsapi_userinfo' ); ctx.redirect(redirectUrl); }); // 处理授权回调 router.get('/wechat/callback', async (ctx, next) => { const code = ctx.query.code; const token = await client.getAccessToken(code); const openid = token.data.openid; const userInfo = await client.getUser(openid); // TODO: 处理用户信息 }); ``` 4. 在Vue项目中,提供一个“使用微信登录”的按钮,点击该按钮时,重定向到后端服务器中的授权接口,实现用户授权登录。 ``` <template> <div> <button @click="loginWithWechat">使用微信登录</button> </div> </template> <script> export default { methods: { loginWithWechat() { window.location.href = 'http://your-server/wechat/login'; } } } </script> ``` 需要注意的是,使用微信OAuth2.0授权登录,需要用户在微信客户端中进行操作,因此需要在移动端或微信公众号中使用。同时,需要在微信公众平台或开放平台中配置授权回调地址,并保证该地址可以被访问到。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值