QQ第三方登录的时候,显示非官方正版应用,报100044错误;
坑1:我们在QQ互联注册成功后需要设置包名和签名,签名是通过官方提供的工具生成的。注意一点:这里的签名是需要由打包签名之后APK生成,我们测试的时候是没有经过签名的,所以一般我们至少在这里需要添加两个签名:一个我们开发测试的APP直接通过官方工具生成的签名,另一个使我们APP经过我们自己的签名文件签名打包后通过官方提供的工具生成的签名;
@Override public void onClick(View view) { switch (view.getId()) { case R.id.iv_login_qq://点击qq登录 onClickLogin(); break; } }
onClickLogin()方法:
private void onClickLogin() { if (!mTencent.isSessionValid()) { mTencent.login(this, "all", loginListener); isServerSideLogin = false; } else { if (isServerSideLogin) { // Server-Side 模式的登陆, 先退出,再进行SSO登陆 mTencent.logout(this); mTencent.login(this, "all", loginListener); isServerSideLogin = false; return; } mTencent.logout(this); }
loginListener接口:
IUiListener loginListener = new BaseUiListener() { @Override protected void doComplete(JSONObject values) { Timestamp = SystemClock.elapsedRealtime() + ""; Log.d(TAG, "AuthorSwitch_SDK:" + SystemClock.elapsedRealtime()); initOpenidAndToken(values);//获取登录返回信息token等 updateUserInfo();//异步更新保存用户信息 } };
initOpenidAndToken()方法:
public void initOpenidAndToken(JSONObject jsonObject) { try { token = jsonObject.getString(Constants.PARAM_ACCESS_TOKEN); expires = jsonObject.getString(Constants.PARAM_EXPIRES_IN); openId_qq = jsonObject.getString(Constants.PARAM_OPEN_ID); Log.d(TAG, "initOpenidAndToken: token:" + token); Log.d(TAG, "initOpenidAndToken: expires:" + expires); Log.d(TAG, "initOpenidAndToken: openId:" + openId_qq); if (!TextUtils.isEmpty(token) && !TextUtils.isEmpty(expires) && !TextUtils.isEmpty(openId_qq)) { mTencent.setAccessToken(token, expires); mTencent.setOpenId(openId_qq); } } catch (Exception e) { } }
updateUserInfo()方法:
private void updateUserInfo() { if (mTencent != null && mTencent.isSessionValid()) { IUiListener listener = new IUiListener() { @Override public void onError(UiError e) { Log.d(TAG, "onError: "); } @Override public void onComplete(final Object response) { new Thread() { @Override public void run() { JSONObject json = (JSONObject) response;if (json.has("figureurl")) { try { String userName = json.getString("nickname"); Log.d(TAG, "run: " + userName); String userIcon = json.getString("figureurl_qq_2"); Log.d(TAG, "run: " + userIcon); String gender = json.getString("gender"); String province = json.getString("province"); String city = json.getString("city"); SharedPreferences user_login = LoginActivity.this.getSharedPreferences("user_login", MODE_PRIVATE); SharedPreferences.Editor edit = user_login.edit(); edit.putString("user_name", userName); edit.putString("user_pic", userIcon); edit.putString("gender", gender); edit.putString("province", province); edit.putString("city", city); edit.putString("openid_qq", openId_qq); edit.commit(); mHandler.sendEmptyMessage(POST_LOGIN_INIFO); } catch (JSONException e) { } } } }.start(); } @Override public void onCancel() { Log.d(TAG, "onCancel: "); } }; mInfo = new UserInfo(this, mTencent.getQQToken()); mInfo.getUserInfo(listener); } else { // mUserInfo.setText(""); // mUserInfo.setVisibility(android.view.View.GONE); // mUserLogo.setVisibility(android.view.View.GONE); } }
必须重写onActivityResult()方法
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d(TAG, "-->onActivityResult:requestCode= " + requestCode + " resultCode=" + resultCode); if (requestCode == Constants.REQUEST_LOGIN || requestCode == Constants.REQUEST_APPBAR) { Tencent.onActivityResultData(requestCode, resultCode, data, loginListener); AppShared.setUserLogin(this, true); ToastUtils.toast("登录成功"); } super.onActivityResult(requestCode, resultCode, data); }
一个建议:用户头像建议保存用户头像链接后再直接保存bitmap,在onResume中下载Bitmap图片,这样即使你设置了清楚缓存,在返回给页面时也会重新去获取用户头像