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

Is the valuation of Meizu at US$6 billion reasonable?

It is well known that Meizu has fully learned Xia...

B2B industry content marketing, how to produce high-quality B2B content?

This article is the content production part of th...

How to effectively deliver short video information flow?

This article mainly introduces the preparations b...

The teacher was asked to resign and was sued for 420,000 yuan!

Article 23: An employer and an employee may agree...

How do Xiaohongshu, Zhihu, etc. acquire seed users?

Seed users refer to those who actively interact w...

Unbelievable! First discovery of Antarctic snow…

Researchers have discovered microplastics for the...

To master fission and increase followers, just grasp these 3 points!

Since the fission processes are similar, you can ...

Tik Tok, are they really the next operational frontier?

Since April, I have repeatedly heard that the ope...

Ctrip Air Ticket iOS Widget Practice

Author | Derek Yang, senior R&D manager at Ct...