使用 UMeng 实现第三方QQ登录
操作流程见下网址:
http://dev.umeng.com/social/android/detail-share/login-page#oauth_2_4
附代码:
1 package com.don.umeng_sina; 2 3 import java.util.Map; 4 import java.util.Set; 5 6 import android.app.Activity; 7 import android.content.Intent; 8 import android.os.Bundle; 9 import android.text.TextUtils; 10 import android.view.View; 11 import android.widget.Toast; 12 13 import com.umeng.socialize.bean.SHARE_MEDIA; 14 import com.umeng.socialize.controller.UMServiceFactory; 15 import com.umeng.socialize.controller.UMSocialService; 16 import com.umeng.socialize.controller.listener.SocializeListeners.UMAuthListener; 17 import com.umeng.socialize.controller.listener.SocializeListeners.UMDataListener; 18 import com.umeng.socialize.exception.SocializeException; 19 import com.umeng.socialize.sso.SinaSsoHandler; 20 import com.umeng.socialize.sso.UMQQSsoHandler; 21 import com.umeng.socialize.sso.UMSsoHandler; 22 import com.umeng.socialize.utils.Log; 23 import com.umeng.soexample.commons.Constants; 24 25 public class MainActivity extends Activity { 26 27 // 整个平台的Controller, 负责管理整个SDK的配置、操作等处理 28 private UMSocialService mController = UMServiceFactory 29 .getUMSocialService(Constants.DESCRIPTOR); 30 31 @Override 32 protected void onCreate(Bundle savedInstanceState) { 33 super.onCreate(savedInstanceState); 34 setContentView(R.layout.activity_main); 35 // 设置新浪SSO handler 36 // mController.getConfig().setSsoHandler(new SinaSsoHandler()); 37 SinaSsoHandler sinaHandler = new SinaSsoHandler(); 38 sinaHandler.addToSocialSDK(); 39 40 // App Key:2442672255 41 // App Secret:b991048880d4645eea758ea1b4c78fde 42 43 // 参数1为当前Activity, 参数2为开发者在QQ互联申请的APP ID,参数3为开发者在QQ互联申请的APP kEY. 44 UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(MainActivity.this, 45 "1103529992", "xIZ38jTDIv1IzB05"); 46 qqSsoHandler.addToSocialSDK(); 47 } 48 49 public void SinaLogin(View v) { 50 // login(SHARE_MEDIA.SINA); 51 52 mController.doOauthVerify(MainActivity.this, SHARE_MEDIA.SINA, 53 new UMAuthListener() { 54 @Override 55 public void onError(SocializeException e, 56 SHARE_MEDIA platform) { 57 } 58 59 @Override 60 public void onComplete(Bundle value, SHARE_MEDIA platform) { 61 if (value != null 62 && !TextUtils.isEmpty(value.getString("uid"))) { 63 Toast.makeText(MainActivity.this, "授权成功.", 64 Toast.LENGTH_SHORT).show(); 65 } else { 66 Toast.makeText(MainActivity.this, "授权失败", 67 Toast.LENGTH_SHORT).show(); 68 } 69 } 70 71 @Override 72 public void onCancel(SHARE_MEDIA platform) { 73 } 74 75 @Override 76 public void onStart(SHARE_MEDIA platform) { 77 } 78 }); 79 } 80 81 /** 82 * 添加QQ登录按钮的监听事件 83 * @param v 84 */ 85 public void QQLogin(View v) { 86 mController.doOauthVerify(MainActivity.this, SHARE_MEDIA.QQ, 87 new UMAuthListener() { 88 @Override 89 public void onStart(SHARE_MEDIA platform) { 90 Toast.makeText(MainActivity.this, "授权开始", 91 Toast.LENGTH_SHORT).show(); 92 } 93 94 @Override 95 public void onError(SocializeException e, 96 SHARE_MEDIA platform) { 97 Toast.makeText(MainActivity.this, "授权错误", 98 Toast.LENGTH_SHORT).show(); 99 } 100 101 @Override 102 public void onComplete(Bundle value, SHARE_MEDIA platform) { 103 Toast.makeText(MainActivity.this, "授权完成", 104 Toast.LENGTH_SHORT).show(); 105 String uid = value.getString("uid"); 106 //如果授权完成,则获取授权平台的用户信息 107 if(!TextUtils.isEmpty(uid)){ 108 getUserInfo(SHARE_MEDIA.QQ); 109 }else{ 110 Toast.makeText(MainActivity.this,"授权失败", 0).show(); 111 } 112 } 113 114 @Override 115 public void onCancel(SHARE_MEDIA platform) { 116 Toast.makeText(MainActivity.this, "授权取消", 117 Toast.LENGTH_SHORT).show(); 118 } 119 }); 120 } 121 122 123 /** 124 * 获取授权平台的用户信息 125 */ 126 private void getUserInfo(SHARE_MEDIA platform) { 127 mController.getPlatformInfo(MainActivity.this, SHARE_MEDIA.SINA, 128 new UMDataListener() { 129 @Override 130 public void onStart() { 131 Toast.makeText(MainActivity.this, "获取平台数据开始...", 132 Toast.LENGTH_SHORT).show(); 133 } 134 135 @Override 136 public void onComplete(int status, Map<String, Object> info) { 137 if (status == 200 && info != null) { 138 StringBuilder sb = new StringBuilder(); 139 Set<String> keys = info.keySet(); 140 for (String key : keys) { 141 sb.append(key + "=" + info.get(key).toString() 142 + "\r\n"); 143 } 144 Log.v("TAG", sb.toString()); 145 } else { 146 Log.v("TAG", "发生错误:" + status); 147 } 148 } 149 }); 150 151 } 152 153 }