安卓微信第三方登陆

在微信开放平台注册应用后拿到APPID与Appsecret
官方地址:https://open.weixin.qq.com

1、添加依赖

{
  api 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'
}

2、添加权限





3、将应用程序注册到微信

public class Application extends android.app.Application {

    private IWXAPI api;

    @Override
    public void onCreate() {
        super.onCreate();
        api = WXAPIFactory.createWXAPI(this, Constant.WX_APPID, false);
        api.registerApp(Constant.WX_APPID);
    }
}

4、请求code

    private void getWeChatCode() {
        if (api == null) {
            api = WXAPIFactory.createWXAPI(this, Constant.WX_APPID, false);
        }
        if (!api.isWXAppInstalled()) {
            Toast.makeText(MainActivity.this, "请先安装微信客户端", Toast.LENGTH_SHORT).show();
        } else {
            SendAuth.Req req = new SendAuth.Req();
            req.scope = "snsapi_userinfo";
            req.state = "wechat_test_state";
            api.sendReq(req);
        }
    }

5、创建wxapi包并在包下创建WXEntryActivity类实现IWXAPIEventHandler接口


public class WXEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {

    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, Constant.WX_APPID, false);
        api.handleIntent(getIntent(), this);
    }


    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        switch (baseResp.errCode) {
            //成功
            case BaseResp.ErrCode.ERR_OK:
                String code = ((SendAuth.Resp) baseResp).code;
                //通过code获取access_token
                getAccessToken(code);
                finish();
                break;

            //取消
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                finish();
                break;

            //拒绝授权
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                finish();
                break;

            default:
                finish();
                break;
        }
    }


    /*
    * https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
    * */

    private void getAccessToken(String code) {
        //拼接网络Url
        StringBuffer loginUrl = new StringBuffer();
        loginUrl.append("https://api.weixin.qq.com/sns/oauth2/access_token")
                .append("?appid=")
                .append(Constant.WX_APPID)//AppId
                .append("&secret=")
                .append(Constant.WX_APPSecret)//AppSecret
                .append("&code=")
                .append(code)
                .append("&grant_type=authorization_code");


        //网络请求成功之后返回数据
        String jsonData = response.body().string();
        try {
            JSONObject jsonObject = new JSONObject(jsonData);
            String access_token = jsonObject.getString("access_token");
            String openId = jsonObject.getString("openid");

            //获取用户个人信息
            getUserInfo(access_token, openId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    
    
    /*
    * https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID
    * */
    
	//获取用户个人信息
    private void getUserInfo(String access_token, String openid) {
        String getUserInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access + "&openid=" + openid;
        
        //网络请求成功之后返回数据
        String userData = response.body().string();
        //将数据存储到本地
        SpUtils.putString(WXEntryActivity.this,Constant.USER_INFO,userData);
    }
}

6、AndroidManifest添加

    

7、从本地读取用户信息

   @Override
    protected void onResume() {
        super.onResume();
        //从本地数据获取用户信息
        String userInfo = SpUtils.getString(MainActivity.this, Constant.USER_INFO, "");
        if (!TextUtils.isEmpty(userInfo)) {
            try {
                JSONObject jsonObject = new JSONObject(userInfo);
                String nickname = jsonObject.getString("nickname");
                String sex = jsonObject.getString("sex");
                String province = jsonObject.getString("province");
                String city = jsonObject.getString("city");
                String country = jsonObject.getString("country");
                String headimgurl = jsonObject.getString("headimgurl");
                String privilege = jsonObject.getString("privilege");
                String unionid = jsonObject.getString("unionid");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

你可能感兴趣的:(Android,android)