Android開發第三方登入--QQ登入
阿新 • • 發佈:2019-02-05
QQ登入、微信登入,新浪微博登入資料獲取demo下載
http://download.csdn.net/detail/pkandroid/9903796
github地址 進入
https://github.com/HYVincent/Login
長話短說。。。
1、建立APP,獲取APP_ID和APP_KEY,當然,首先得有個賬號**
2、下載sdk 點選去下載
3、導包(我的是AndroidStudio 2.3.2)解壓下載的sdk,把mta-sdk-1.6.2.jar和open_sdk_r5788_lite.jar複製到lib下(注意區分open_sdk_r5788_lite.jar和open_sdk_r5788.jar的區別,我用了QQ登入,因此基礎班就夠用了),然後選中郵件選擇add as Library….
4、配置AndroidManifest 注意**要替換為自己申請的APP_ID
需要一下許可權
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!--QQ登入配置 start-->
<activity
android:name="com.tencent.tauth.AuthActivity"
android:noHistory="true"
android:launchMode="singleTask" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="tencent******" />
</intent-filter>
</activity>
<!--SDK_V2.0引入了AssistActivity,開發者需在androidManifest.xml中註冊。程式碼如下:-->
<activity android:name="com.tencent.connect.common.AssistActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:configChanges="orientation|keyboardHidden|screenSize" />
<!--QQ登入配置 end-->
4、在LoginActivity中初始化並使用
public class LoginActivity extends BaseActivity implements LoginView.View {
.........
private Tencent mTencent;
private UserInfo userInfo;
private BaseUiListener listener = new BaseUiListener();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
.....
// Tencent類是SDK的主要實現類,開發者可通過Tencent類訪問騰訊開放的OpenAPI。
// 其中APP_ID是分配給第三方應用的appid,型別為String。
mTencent = Tencent.createInstance(Config.QQ_LOGIN_APP_ID, this.getApplicationContext());
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0) {
JSONObject response = (JSONObject) msg.obj;
if (response.has("nickname")) {
Gson gson=new Gson();
QQUser user=gson.fromJson(response.toString(),QQUser.class);
if (user!=null) {
ViseLog.d("userInfo:暱稱:"+user.getNickname()+" 性別:"+user.getGender()+" 地址:"+user.getProvince()+user.getCity());
ViseLog.d("頭像路徑:"+user.getFigureurl_qq_2());
// Picasso.with(MainActivity.this).load(response.getString("figureurl_qq_2")).into(imageView);
}
}
}
}
};
@OnClick({R.id.login_tv_register, R.id.login_btn_go, R.id.login_tv_forget_password,R.id.iv_other_login_qq,R.id.iv_other_login_sina,R.id.iv_other_login_wx})
public void onViewClicked(View view) {
switch (view.getId()) {
......
case R.id.iv_other_login_qq:
/**
吊起QQ登入
*/
if (!mTencent.isSessionValid())
{
//登出登入 mTencent.logout(this);
mTencent.login(this, "all", listener);
}
break;
......
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Tencent.onActivityResultData(requestCode,resultCode,data,new BaseUiListener());
}
// 實現回撥 IUiListener
//呼叫SDK已經封裝好的介面時,例如:登入、快速支付登入、應用分享、應用邀請等介面,需傳入該回調的例項。
class BaseUiListener implements IUiListener {
@Override
public void onComplete(Object o) {
ViseLog.d("result:"+o.toString());
try {
JSONObject jsonObject = new JSONObject(o.toString());
initOpenidAndToken(jsonObject);
updateUserInfo();
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onError(UiError e) {
showMsg(0,"onError:code:" + e.errorCode + ", msg:"
+ e.errorMessage + ", detail:" + e.errorDetail);
}
@Override
public void onCancel() {
showMsg(2,"onCancel");
}
}
/**
* 獲取登入QQ騰訊平臺的許可權資訊(用於訪問QQ使用者資訊)
* @param jsonObject
*/
public void initOpenidAndToken(JSONObject jsonObject) {
try {
String token = jsonObject.getString(Constants.PARAM_ACCESS_TOKEN);
String expires = jsonObject.getString(Constants.PARAM_EXPIRES_IN);
String openId = jsonObject.getString(Constants.PARAM_OPEN_ID);
if (!TextUtils.isEmpty(token) && !TextUtils.isEmpty(expires)
&& !TextUtils.isEmpty(openId)) {
mTencent.setAccessToken(token, expires);
mTencent.setOpenId(openId);
}
} catch(Exception e) {
}
}
private void updateUserInfo() {
if (mTencent != null && mTencent.isSessionValid()) {
IUiListener listener = new IUiListener() {
@Override
public void onError(UiError e) {
}
@Override
public void onComplete(final Object response) {
Message msg = new Message();
msg.obj = response;
Log.i("tag", response.toString());
msg.what = 0;
mHandler.sendMessage(msg);
}
@Override
public void onCancel() {
}
};
userInfo = new UserInfo(this, mTencent.getQQToken());
userInfo.getUserInfo(listener);
}
}
}
5、登入結果:
授權結果:
result:{"ret":0,"openid":"A3F4F2111DFBD37E2A5250A9B805868","access_token":"5E8D0507F17D9295C916FE6F401AE6","pay_token":"90DB41BD1A367F8FBAD06EDC9FADB80","expires_in":7776000,"pf":"desktop_m_qq-10000144-android-2002-","pfkey":"cc45091bfc5811636989a47db68ec5fc","msg":"","login_cost":143,"query_authority_cost":296,"authority_cost":0}
格式化一下:
登入結果:
{
"ret":0,
"msg":"",
"is_lost":0,
"nickname":"習慣寵著你",
"gender":"男",
"province":"廣東",
"city":"深圳",
"figureurl":"http://qzapp.qlogo.cn/qzapp/1106210336/A3F4F2111DF37E2A52502A9B805868/30",
"figureurl_1":"http://qzapp.qlogo.cn/qzapp/1106210336/A3F4F2111DFBD2A52502A9B805868/50",
"figureurl_2":"http://qzapp.qlogo.cn/qzapp/1106210336/A3F4F2111DFB7E2A52502A9B805868/100",
"figureurl_qq_1":"http://q.qlogo.cn/qqapp/1106210336/AF4F2111DFBD37E2A5502A9B805868/40",
"figureurl_qq_2":"http://q.qlogo.cn/qqapp/1106210336/A3F4F2111DFBD37E2A502A9B805868/100",
"is_yellow_vip":"0",
"vip":"0",
"yellow_vip_level":"0",
"level":"0",
"is_yellow_year_vip":"0"
}