Third-party login

Third-party login

Source code introduction: Because it involves the personal information security of customers, you need to make simple modifications according to this blog (http://blog.csdn.net/csdnfml/article/details/49446479)

Source code effect:

Source code snippet:

  1. /** ------------------------QQ third-party login-------------------- */  
  2. public   void loginQQ(){
  3. /** Check if you have logged in */  
  4. if (!mTencent.isSessionValid()){
  5. mTencent.login( this , "all" ,loginListener);
  6. } /** Login after logging out */  
  7. else {
  8. mTencent.logout( this );
  9. mTencent.login( this , "all" ,loginListener);
  10. }
  11. }
  12. IUiListener loginListener = new BaseUiListener() {
  13. @Override  
  14. protected   void doComplete(JSONObject values) {
  15. initOpenidAndToken(values);
  16. updateUserInfo();
  17. }
  18. };
  19. /** QQ login step 2: store token and openid */  
  20. public   static   void initOpenidAndToken(JSONObject jsonObject) {
  21. try {
  22. String token = jsonObject.getString(Constants.PARAM_ACCESS_TOKEN);
  23. String expires = jsonObject.getString(Constants.PARAM_EXPIRES_IN);
  24. String openId = jsonObject.getString(Constants.PARAM_OPEN_ID);
  25. if (!TextUtils.isEmpty(token) && !TextUtils.isEmpty(expires) && !TextUtils.isEmpty(openId)) {
  26. mTencent.setAccessToken(token, expires);
  27. mTencent.setOpenId(openId);
  28. }
  29. } catch (Exception e) {
  30. }
  31. }
  32. /** QQ login step 3: Get user information */  
  33. private   void updateUserInfo() {
  34. if (mTencent != null && mTencent.isSessionValid()) {
  35. IUiListener listener = new IUiListener() {
  36. @Override  
  37. public   void onError(UiError e) {
  38. Message msg = new Message();
  39. msg.obj = "Change the mobile phone time to obtain network time" ;
  40. msg.what = 1 ;
  41. mHandler.sendMessage(msg);
  42. }
  43.   
  44. @Override  
  45. public   void onComplete( final Object response) {
  46. Message msg = new Message();
  47. msg.obj = response;
  48. msg.what = 0 ;
  49. mHandler.sendMessage(msg);
  50. }
  51. @Override  
  52. public   void onCancel() {
  53. Message msg = new Message();
  54. msg.obj = "Failed to obtain user information" ;
  55. msg.what = 2 ;
  56. mHandler.sendMessage(msg);
  57. }
  58. };
  59. mInfo = new UserInfo( this , mTencent.getQQToken());
  60. mInfo.getUserInfo(listener);
  61. } else {
  62.   
  63. }
  64. }
  65. Handler mHandler = new Handler() {
  66. @Override  
  67. public   void handleMessage(Message msg) {
  68. if (msg.what == 0 ) {
  69. JSONObject response = (JSONObject) msg.obj;
  70. if (response.has( "nickname" )) {
  71. try {
  72. log( "User information is obtained successfully, and the result is returned: " + response.toString());
  73. mThirdLoginResult.setText( "Login successful\n" + "Nickname:" +response.getString( "nickname" )+ "\nAvatar address:" +response.get( "figureurl_qq_1" ));
  74. } catch (JSONException e) {
  75. e.printStackTrace();
  76. }
  77. }
  78. } else   if (msg.what == 1 ){
  79. mThirdLoginResult.setText(msg+ "" );
  80. } else   if (msg.what == 2 ){
  81. mThirdLoginResult.setText(msg+ "" );
  82. }
  83. }
  84.   
  85. };
  86. /** QQ login step ***: Get token and openid */  
  87. private   class BaseUiListener implements IUiListener {
  88. @Override  
  89. public   void onComplete(Object response) {
  90. if ( null == response ) {
  91. mToast( "Login failed" );
  92. return ;
  93. }
  94. JSONObject jsonResponse = (JSONObject) response;
  95. if ( null != jsonResponse && jsonResponse.length() == 0 ) {
  96. mToast( "Login failed" );
  97. return ;
  98. }
  99. log( "QQ login successfully returned result -" + response.toString());
  100. doComplete((JSONObject)response);
  101. }
  102. protected   void doComplete(JSONObject response) {}
  103. @Override  
  104. public   void onError(UiError e) {
  105. Util.toastMessage(MainActivity. this , "onError: " + e.errorDetail);
  106. Util.dismissDialog();
  107. }
  108. @Override  
  109. public   void onCancel() {
  110. Util.toastMessage(MainActivity. this , "onCancel: " );
  111. Util.dismissDialog();
  112. if (isServerSideLogin) {
  113. isServerSideLogin = false ;
  114. }
  115. }
  116. }
  117. /** -------------------------QQ third-party login ends-------------------- */  

Download address: http://download..com/data/2115240

<<:  Imitate IOS Segment, search box, switch button and time selection

>>:  Android App Login Screen

Recommend

NASA releases more cosmic images from Webb Space Telescope

After US President Biden released the first full-...

The secrets of B station's operation strategy

In recent years, from the marketing of WeChat pub...