1,首先在微信开放平台上注册应用,填写相关信息,获取appid
2,把固定参数配置搭配到常量文件或gradle中
buildConfigField "String","WEIXIN_KEY",'""'// 微信登录appid
buildConfigField "String","WEIXIN_SCOPE",'"snsapi_userinfo"'// 微信登录scope
buildConfigField "String","WEIXIN_STATE",'"wechat_sdk_demo_test_neng"'// 微信登录state
3,在工程目录下创建wxapi文件夹,并在文件夹内创建WXEntyActivity文件
4,WXEntyActivity文件内容
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IWXAPI api = WXAPIFactory.createWXAPI(this, BuildConfig.WEIXIN_KEY, true);
api.handleIntent(getIntent(),this);
finish();
}
// 微信发送请求到第三方应用时,会回调到该方法
@Override
public void onReq(BaseReq baseReq) {
}
@Override
public void onResp(BaseResp baseResp) {
String result;
switch (baseResp.errCode) {
case BaseResp.ErrCode.ERR_OK:
result = "登录成功";
break;
case BaseResp.ErrCode.ERR_AUTH_DENIED:
result = "用户拒绝授权";
break;
case BaseResp.ErrCode.ERR_USER_CANCEL :
result = "用户取消";
break;
default:
result = "失败";
break;
}
if (result != null) {
Toast.makeText(this, baseResp.errCode + result, Toast.LENGTH_SHORT).show();
}
}
}
5,创建微信登录的工具类
public class WxLoginUtil {
public static IWXAPI api;
public static Context mContext;
/**
* 初始化
*/
public static void initWx(Context context) {
mContext = context;
api = WXAPIFactory.createWXAPI(context, BuildConfig.WEIXIN_KEY, true);
api.registerApp(BuildConfig.WEIXIN_KEY);
final IWXAPI msgApi = WXAPIFactory.createWXAPI(context, null);
// 将该app注册到微信
msgApi.registerApp(BuildConfig.WEIXIN_KEY);
}
/**
* 微信登录
*/
public static void longinWx() {
if (mContext == null) {
Toast.makeText(mContext, "未初始化", Toast.LENGTH_SHORT).show();
return;
}
if (!api.isWXAppInstalled()) {
Toast.makeText(mContext, "未安装微信客户端", Toast.LENGTH_SHORT).show();
return;
}
final SendAuth.Req req = new SendAuth.Req();
req.scope = BuildConfig.WEIXIN_SCOPE;
req.state = BuildConfig.WEIXIN_STATE;
api.sendReq(req);
}
}
6,在application的onCreate的方法中调用初始化方法
public class BaseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
context = this;
WxLoginUtil.initWx(this);
}
7,调用微信登录
WxLoginUtil.longinWx();
8,获取微信登录成功之后的用户信息
/**
* 获取openid accessToken值用于后期操作
*
* @param code 请求码
*/
private void getAccess_token(final String code) {
String path = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="
+ WxData.WEIXIN_APP_ID
+ "&secret="
+ WxData.APP_SECRET
+ "&code="
+ code
+ "&grant_type=authorization_code";
OkHttpUtils.get().url(path).build().execute(new StringCallback() {
@Override
public void onError(okhttp3.Call call, Exception e) {
}
@Override
public void onResponse(String response) {
Log.e("-----", "onResponse: " + response);
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(response);
String openid = jsonObject.getString("openid").toString().trim();
String access_token = jsonObject.getString("access_token").toString().trim();
getUserMesg(access_token, openid);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
/**
* 获取微信的个人信息
*
* @param access_token
* @param openid
*/
private void getUserMesg(final String access_token, final String openid) {
String path = "https://api.weixin.qq.com/sns/userinfo?access_token="
+ access_token
+ "&openid="
+ openid;
OkHttpUtils.get().url(path).build().execute(new StringCallback() {
@Override
public void onError(okhttp3.Call call, Exception e) {
}
@Override
public void onResponse(String response) {
Log.e("------", "全部数据: " + response);
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(response);
String nickname = jsonObject.getString("nickname");
int sex = Integer.parseInt(jsonObject.get("sex").toString());
String headimgurl = jsonObject.getString("headimgurl");
String openid1 = jsonObject.getString("openid");
Log.e("---", "用户基本信息:");
Log.e("---", "nickname:" + nickname);
Log.e("---", "sex: " + sex);
Log.e("---", "headimgurl:" + headimgurl);
} catch (JSONException e) {
e.printStackTrace();
UIUtils.runOnUIToast("登陆错误,请重新再试");
}
// finish();
}
});
}