android微信第三方登录怎么通过code获取openid?
方法1:
1.登录公众账号设置OAuth2.0
2.设置菜单按钮URL为OAuth链接
3.页面后台获取:
publicStringgetopenId(){HttpServletRequestrequest=ServletActionContext.getRequest();HttpServletResponseresponse=ServletActionContext.getResponse();response.setContentType("text/html");Stringcode=request.getParameter("code");Stringurlstr="https://api.weixin.qq.com/sns/oauth2/access_token?appid=<appId>&secret=<secret>&code="+code+"&grant_type=authorization_code";JSONObjectjson;try{json=JSONObject.fromObject(HTTPTools.postToGetJson(urlstr));openId=json.getString("openid");}catch(Exceptione){//e.printStackTrace();return"";}returnopenId;}
方法2:
//重写onresume()方法
@Override
protected void onResume() {
if (type != null && type.equals("mwx")) {
SharedPreferences settings = getSharedPreferences("setting", 0);
String code = settings.getString("code", null);
if (code != null && !code.equals("")) {
showProgress(true);
getOpenid(code);
}
settings.edit().clear();
settings.edit().commit();
}
super.onResume();
}
// 获取微信用户的openid和access token
public void getOpenid(String code) {
final AsyncHttpClient httpClient = Gl.sharedAsyncClient();
RequestParams params = new RequestParams();
params.put("appid", Constants.wxAPP_ID);
params.put("secret", Constants.wxAppSecret);
params.put("code", code);
params.put("grant_type", "authorization_code");
String httpurl = "https://api.weixin.qq.com/sns/oauth2/access_token";
httpClient.get(httpurl, params, new JsonHttpResponseHandler() {
@OverridepublicvoidonSuccess(intstatusCode,Header[]headers,JSONObjectresponse){try{Stringopendid=response.getString("openid");if(opendid!=null&&!opendid.equals("")){openid=response.getString("openid");otherLogin("mwx",opendid);}}catch(JSONExceptione){e.printStackTrace();}super.onSuccess(statusCode,headers,response);}@OverridepublicvoidonFailure(intstatusCode,Header[]headers,StringresponseString,Throwablethrowable){super.onFailure(statusCode,headers,responseString,throwable);}});}
更多相关文章
- Android打开系统相册的问题
- android——OpenGL(2)——gl10方法解析
- Android设备路径及容量的读取
- Android(安卓)Camera FW到Camera Hal调用流程
- android之VideoView和视频播放View的扩展
- Android动画TweenAnimation的使用方法
- android 5.1 USB调试开关
- 【Android】小白进阶之接口和抽象类的使用
- Android与单片机通信常用数据转换方法总结