android第三方登录微信
‘壹’ android 微信第三方登录怎么通过code获取openid
1.登录公众账号设置OAuth2.0
2.设置菜单按钮URL为OAuth链接
3.页面后台获取:
public String getopenId() {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html");
String code = request.getParameter("code");
String urlstr = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=<appId>&secret=<secret>&code=" + code + "&grant_type=authorization_code";
JSONObject json;
try {
json = JSONObject.fromObject(HTTPTools.postToGetJson(urlstr));
openId = json.getString("openid");
} catch (Exception e) {
// e.printStackTrace();
return "";
}
return openId;
}
‘贰’ 第三方微信登录安卓接口实现
1)在build.gradle的dependencies中添加2、申明应用权限3、向微信注册APP4、请求微信授权登录5、创建WXEntryActivity.java来接收微信的响应事件这里和签名一样,很重要,你如果名字错了,或者包名的位置错了,都是不能回调的,切记
‘叁’ Android微信第三方登录/支付,没有走回调
1、要正式签名的apk,使用debug包貌似无法调动微信
2、清单文件中要注册微信回调
<activity
android:name=".wxapi.WXEntryActivity"
android:exported="true"
android:label="@string/app_name"
android:launchMode="singleTop"
android:theme="@android:style/Theme.Translucent" />
3、回调的类名,一定要是官网的WXEntryActivity.java。这里很迷,我之前名字是WXPayEntryActivity,因为之前使用了微信支付,就起了这个名字,后续又加入了微信分享,也没有问题,当我又加入微信登录之后,就不走回调了,改成WXEntryActivity.java成功走了回调。
4、回调类所在包名,一定要是wxapi:
.wxapi.WXEntryActivity