前言 App联合登陆很常用,包括使用qq、微信、微博账号登陆等,这篇记录下微信登陆的接入流程。 接入流程
https://open.weixin.qq.com/cgi-bin/frame?t=home/app_tmpl&lang=zh_CN资源中心点击移动应用,点击微信登录
1.导入libammsdk.jar的jar包
2.初始化IWXAPI
// 通过WXAPIFactory工厂,获取IWXAPI的实例 api = WXAPIFactory.createWXAPI(this, G.AppId_WX,true); 3.发送认证请求
if( !api.isWXAppInstalled()){
Toast.makeText(context, "请先安装微信应用", Toast.LENGTH_SHORT).show();
return false;
}
if(!api.isWXAppSupportAPI() ){
Toast.makeText(context, "请先更新微信应用", Toast.LENGTH_SHORT).show();
return false;
}
// 将应用的APPID注册到微信
api.registerApp(Constant.WX_APP_ID);
final SendAuth.Req req = newSendAuth.Req();
req.scope = Constant.WX_APP_SCOPE;
req.state = Constant.WX_APP_STATE;
api.sendReq(req);
调起微信客户端让用户进行授权,用户进行操作后会调起...wxapi.WXEntryActivity 这个类,..是应用包名,这个WXEntryActivity需要继承Activity并且实现IWXAPIEventHandler接口,当然,这个WXEntryActivity也需要在清单文件中注册
android:name="**.**.**.wxapi.WXEntryActivity"
android:exported="true"
android:label="@string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait" >
WXEntryActivity先实现IWXAPIEventHandler,然后实现3个方法一个onNewIntent、onReq、onResp在onResp得到code,然后在通过code获取openid和access_token,然后根据这两个参数调自己系统后台,判断是该用户登录过
public classWXEntryActivity extends Activityimplements IWXAPIEventHandler{
privateIWXAPIapi;
String openid,access_token,expires_in;
ContextthisContext;
Intentintent;
Handlerhandler= newHandler(){
@Override
public void handleMessage(Messagemsg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
JSONObjectjsonObject = (JSONObject) msg.obj;
switch (msg.what) {
case 1:
try {
openid = jsonObject.getString("openid");
access_token =jsonObject.getString("access_token");
requestCodeApiData();
}catch(JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
default:
break;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
thisContext = this;
intent = getIntent();
setContentView(R.layout.activity_wx_entry);
api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
api.handleIntent(getIntent(),this);
}
@Override
protected void onNewIntent(Intentintent) {
super.onNewIntent(intent);
setIntent(intent);
api.handleIntent(intent,this);
}
@Override
public void onReq(BaseReq req) {
finish();
}
@Override
public void onResp(BaseResp resp) {
switch (resp.errCode) {
case BaseResp.ErrCode.ERR_OK:
System.out.println("0000");
Stringcode = ((SendAuth.Resp) resp).code;//即为所需的code
System.out.println("code:"+code);
doGet(((SendAuth.Resp)resp).code);
break;
case BaseResp.ErrCode.ERR_AUTH_DENIED:
System.out.println("用户拒绝授权");
break;
case BaseResp.ErrCode.ERR_USER_CANCEL:
System.out.println("用户取消");
break;
default:
break;
}
}
public void doGet(final String code){
new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
URL url;
String link = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+G.AppId_WX+"&secret="+G.AppScrect+"&code="+code+"&grant_type=authorization_code";
byte[] buf = Util.httpGet(link);
if (buf !=null && buf.length > 0) {
Stringcontent = newString(buf);
JSONObject json = JSONObject.parseObject(content);
Messagemessage = newMessage();
message.obj = json;
message.what = 1;
handler.sendMessage(message);
}
}
}.start();
}
}
小心有坑:在获取code时候微信后台返回{“errcode”:40029,”errmsg”:”invalidcode”},我那时获取不到微信返回的json,就将获取的url在logcat上打印,然后放浏览器里面一直返回时{“errcode”:40029,”errmsg”:”invalidcode”},查了半天,发现是其他地方错了,再一个就是微信的同一个code,只请求了一次,我在浏览器上请求相当于多次了就出现了{“errcode”:40029,”errmsg”:”invalidcode”}