Android QQ Third party login

Foreword

Please be quiet, it will be a moment of make Belive B.

Integrated QQ login is an essential part of a project, and today I will record my integration process for more convenient use in future development.

First:APP ID

You need to go to the QQ open platform to create an application, and after success you will get APP ID.
qq

Second:DownLoad

You need to import a jar package and download the address http://wiki.connect.qq.com/sdk%e4%b8%8b%e8%bd%bd
It used to need two jar packages, but now the latest version only needs one.
open

Third:Add Dependecy

Add a dependency and compile a project

Fourth:Permission

Open AndroidManifest.xml add permission

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Sixth:Add Activity And App Id

Add Two Activity to AndroidManifest.xml And replace APP ID with its own APP ID

<activity
            android:name="com.tencent.tauth.AuthActivity"
            android:launchMode="singleTask"
            android:noHistory="true">
            <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="tencent1106500653" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.tencent.connect.common.AssistActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />

Seventh:Edit his own java file

The key code is:

    private static final String APP_ID = "own APP ID";
    Tencent mTencent = Tencent.createInstance(APP_ID, MainActivity.this.getApplicationContext());
    mTencent.login(MainActivity.this,"all",authorizationListener);
    
    private IUiListener authorizationListener = new IUiListener() {
        @Override
        public void onComplete(Object response) {
            Toast.makeText(MainActivity.this, "Empowerment success", Toast.LENGTH_SHORT).show();
            Log.e(TAG, "response:" + response);
            JSONObject obj = (JSONObject) response;
            try {
                String openID = obj.getString("openid");
                String accessToken = obj.getString("access_token");
                String expires = obj.getString("expires_in");
                mTencent.setOpenId(openID);
                mTencent.setAccessToken(accessToken, expires);
                QQToken qqToken = mTencent.getQQToken();
                mUserInfo = new UserInfo(getApplicationContext(), qqToken);
                mUserInfo.getUserInfo(loginUiLisener);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onError(UiError uiError) {
        }

        @Override
        public void onCancel() {
        }
    };

    private IUiListener loginUiLisener = new IUiListener() {
        @Override
        public void onComplete(Object response) {
            Log.e(TAG, "Login Success" + response.toString());
            Toast.makeText(MainActivity.this, "Login Success", Toast.LENGTH_SHORT);
            JSONObject object = (JSONObject) response;
            mNicknameTv.setText("Nickname:" + object.optString("nickname"));
            mGenderTv.setText("Gender:" + object.optString("gender"));
            Picasso.with(MainActivity.this)
                    .load(object.optString("figureurl_qq_2"))
                    .into(mHeadIv);
        }

        @Override
        public void onError(UiError uiError) {
            Log.e(TAG, "Login Fail" + uiError.toString());
            Toast.makeText(MainActivity.this, "Login Fail", Toast.LENGTH_SHORT);
        }

        @Override
        public void onCancel() {
            Log.e(TAG, "Lgoing Cancel");
            Toast.makeText(MainActivity.this, "Lgoing Cancel", Toast.LENGTH_SHORT);
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Constants.REQUEST_LOGIN) {
            Tencent.onActivityResultData(requestCode, resultCode, data, onResultListener);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

Here our QQ login will be successful and return to our data
qqq
So I can see that I am a poor man.
qqlogin
Thank you for your browsing. You need to send me a message from the source code
I wish you no bug in the code, good night!


长路漫漫,菜不是原罪,堕落才是原罪。
我的CSDN:https://blog.csdn.net/wuyangyang_2000
我的简书:https://www.jianshu.com/u/20c2f2c3560a
我的掘金:https://juejin.im/user/58009b94a0bb9f00586bb8a0
我的GitHub:https://github.com/wuyang2000
个人网站:http://www.xiyangkeji.cn
个人app(茜茜)蒲公英连接:https://www.pgyer.com/KMdT
我的微信公众号:茜洋 (定期推送优质技术文章,欢迎关注)
Android技术交流群:691174792

以上文章均可转载,转载请注明原创。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值