微信開放平臺https://open.weixin.qq.com/cgi-bin/index?t=home/index&lang=zh_CNandroid
微信審覈在1-3個工做日內完成json
集成步驟文檔寫的挺清楚,這裏只貼關鍵代碼api
調起微信認證頁面,在微信響應後獲取到code微信
public void reqPass() { final SendAuth.Req req = new SendAuth.Req(); req.scope = "snsapi_userinfo"; req.state = "bbbl_wx_login_test"; if (api != null) { api.sendReq(req); Toast.makeText(getBaseContext(), "請求拉起微信認證頁面", LENGTH_SHORT).show(); } else { Toast.makeText(getBaseContext(), "微信認證走丟了,跳轉至登錄界面,請嘗試再次登錄", Toast.LENGTH_LONG).show(); } }
@Override public void onResp(BaseResp baseResp) { switch (baseResp.errCode) { case BaseResp.ErrCode.ERR_OK://分享成功 if (ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX == baseResp.getType()) { Toast.makeText(WXEntryActivity.this, "分享成功", Toast.LENGTH_LONG).show(); finish(); break; } String code = ((SendAuth.Resp) baseResp).code; reqes(code); break; case BaseResp.ErrCode.ERR_USER_CANCEL://分享取消 Toast.makeText(WXEntryActivity.this, "您選擇了取消", Toast.LENGTH_LONG).show(); finish(); break; case BaseResp.ErrCode.ERR_AUTH_DENIED://分享拒絕 Toast.makeText(WXEntryActivity.this, "您選擇了取消", Toast.LENGTH_LONG).show(); finish(); break; } finish(); }
獲取到code後就能夠訪問接口得到access_token,繼而獲得用戶信息app
public void reqes(String code) { RequestQueue mQueue = Volley.newRequestQueue(this); StringRequest stringRequest = new StringRequest(Constants.REQ_URL + "?appid=" + Constants.APP_ID_WX + "&secret=" + Constants.SECRET_WX + "&code=" + code + "&grant_type=authorization_code", new Response.Listener<String>() { @Override public void onResponse(String response) { TokenInfo json = new Gson().fromJson(response, TokenInfo.class); wx_access_token = json.getAccess_token(); wx_openid = json.getOpenid(); getUserInfo(wx_access_token, wx_openid); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); mQueue.add(stringRequest); mQueue.start(); } public void getUserInfo(String access_token, final String openid) { RequestQueue mQueue = Volley.newRequestQueue(this); StringRequest stringRequest = new StringRequest(Constants.USERINFO_URL + "?access_token=" + access_token + "&openid=" + openid, new Response.Listener<String>() { @Override public void onResponse(String response) { Log.i("WXUserInfo", response); WXUserInfo info = new Gson().fromJson(response, WXUserInfo.class); String nickname = info.getNickname(); String headimgurl = info.getHeadimgurl(); //判斷是否爲魅族手機,採用不一樣的編碼,以解決亂碼的問題 if ("Meizu".equals(android.os.Build.MANUFACTURER)) { //魅族手機 try { String en_nickname = URLEncoder.encode(nickname, "iso-8859-1"); String de_nickname = URLDecoder.decode(en_nickname, "utf-8"); bindCustomer(de_nickname, headimgurl, openid, "wx"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } else { try { String en_nickname = URLEncoder.encode(nickname, "utf-8"); String de_nickname = URLDecoder.decode(en_nickname, "utf-8"); bindCustomer(de_nickname, headimgurl, openid, "wx"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); mQueue.add(stringRequest); mQueue.start(); }
獲取用戶信息時,微信文檔說默認是中文,可是也有人遇到是英文的狀況,這時在請求的時候能夠多加一個參數來指定語言ide