1. 程式人生 > >微信第三方掃碼登入

微信第三方掃碼登入

1 專案應用去微信開放平臺申請,不是公眾平臺

2 在頁面中新增微信登入按鈕

需要修改的引數 appid:申請應用的appid,redirect_uri:登入後的回撥地址(回撥地址要在網站應用設定的回撥域名下,不能有埠號,url要經過URLcode轉碼),其他不變

<a href="  https://open.weixin.qq.com/connect/qrconnect?appid=wxed213960e&redirect_uri=https%3a%2f%2frefuge.picp.vip%2fLoginThirdController%2fwxLogin&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect">
   <img src="/images/li_img/weixin.jpg" alt="" class="logo"></a>

3,後臺處理回撥介面編寫,登入成功會回撥redirect_uri,並在地址新增code與state引數,只要code引數就行

@RequestMapping("wxLogin")
public ModelAndView wxLogin(@RequestParam String code, ModelAndView modelAndView){

 4.用返回的code 引數去微信伺服器換取openid(使用者唯一標誌),修改的引數 appid ,secret(申請應用的密碼),code剛取得的code

String url="https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+ secret+"&code="+code+"&grant_type=authorization_code";

可用httpclient請求。返回json字串,用fastjson解析,並提取openid引數

JSONObject jsonObject=  JSONObject.parseObject(result);
Object openid=  jsonObject.get("openid");

拿到使用