使用微信登录时,首先在Application里通过WXAPIFactory工厂,获取IWXAPI的实例再注册微信
<span style="font-size:18px;">// 通过WXAPIFactory工厂,获取IWXAPI的实例
api = WXAPIFactory.createWXAPI(this, Constant.APP_ID, true);
// 注册微信
api.registerApp(Constant.APP_ID);</span>
然后在需要的地方写上应用授权授权登录接入代码
// 注意不要再次注册,不然会调用拍照权限
// wxApi.registerApp(Constant.APP_ID);
isWXLogin = true;
SendAuth.Req req = new SendAuth.Req();
req.scope = "snsapi_userinfo";
req.state = "wechat_sdk_demo_test";
wxApi.sendReq(req);
请注意那个注释,我被坑了挺久的;
然后新建一个包,包名为
在wxapi包名下新建activity命名为WXEntryActivity,并在清单文件中配置android:exported="true"
在WXEntryActivity onCreate中
// 回调onRes用的
LoginActivity.wxApi.handleIntent(getIntent(), this);
if (!LoginActivity.wxApi.isWXAppInstalled())
{
ToastUtil.getShortToastByString(WXEntryActivity.this, "您还未安装微信客户端");
return;
}
然后在onResp中获取code,通过
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code获取access_token
再通过https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN
获取信息
在onNewIntent中
<span style="font-size:18px;"> @Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
setIntent(intent);
}
</span>
这样就搞定了