shareSdk android third party login modular integration

No need to configure the manifest file, no need to import the jar package

Project level

Add the following script to your root module build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    
    dependencies {
        // Registered MobSDK
        classpath "com.mob.sdk:MobSDK:2018.0319.1724"
    }
}

In build.gradle using the corresponding module, add the MobSDK plug-in and extension, such as:

apply plugin: 'com.mob.sdk'

MobSDK {
    appKey "Replace with mob Officially applied appkey"
    appSecret "Replace with mob Officially applied appkey Corresponding appSecret"
    ShareSDK {
        devInfo {
            SinaWeibo {
                appKey "568898243"
                appSecret "38a4f8204cc784f81f9f0daaf31e02e3"
                callbackUri "http://www.sharesdk.cn"
            }
            Wechat {
                appId "wx4868b35061f87885"
                appSecret "64020361b8ec4c99936c0e3999a9f249"
                userName "gh_afb25ac019c9"
                path "pages/index/index.html?id=1"
                withShareTicket true
                miniprogramType 2
            }
            QQ {
                appId "100371282"
                appKey "aed9b0303e3ed1e27bae87c33761161d"
            }
            WechatMoments {
                appId "wx4868b35061f87885"
                appSecret "64020361b8ec4c99936c0e3999a9f249"
            }
            WechatFavorite {
                appId "wx4868b35061f87885"
                appSecret "64020361b8ec4c99936c0e3999a9f249"
            }
        }
    }
}

MobSDK {placed at the bottom

apply plugin: 'com.mob.sdk' is placed at the bottom of the top apply

Because it's modular development, build.gradle of the outermost module (usually the default app) should also write this sentence

apply plugin: 'com.mob.sdk' 

 

 private void loginVx() {
        keyType = "WECHAT";
        Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
        wechat.SSOSetting(true);
        wechat.setPlatformActionListener(this);
        if (!wechat.isClientValid()) {
            Toast.makeText(this, "Wechat not installed,Please install wechat first", Toast.LENGTH_LONG).show();
        }
        authorize(wechat);

    }
/**
     * To grant authorization
     *
     * @param platform
     */
    private void authorize(Platform platform) {
        if (platform == null) {
            return;
        }
        if (platform.isAuthValid()) {  //Delete authorization data if authorized
            platform.removeAccount(true);
        }

        platform.showUser(null); //Authorize and obtain user information
    }
public class LoginActivity extends BaseActivity implements PlatformActionListener

 

 @Override
    public void onComplete(Platform platform, int action, HashMap<String, Object> hashMap) {

        Message message = Message.obtain();
        message.what = 1;
        message.obj = platform;
        mHandler.sendMessage(message);

    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        Message message = Message.obtain();
        message.what = 2;
        message.obj = platform;
        mHandler.sendMessage(message);
    }

    @Override
    public void onCancel(Platform platform, int i) {
        Message message = Message.obtain();
        message.what = 3;
        message.obj = platform;
        mHandler.sendMessage(message);
        ;
    }
 private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    Platform platform = (Platform) msg.obj;
                    loginSucess(platform);
                    break;
                case 2:
                    Toast.makeText(LoginActivity.this, "Login authorization failed", Toast.LENGTH_SHORT).show();
                    break;
                case 3:
                    Toast.makeText(LoginActivity.this, "Authorized login cancellation", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    private void loginSucess(Platform platform) {
        //platform.getDb().exportData() get user data
        // The authorization here successfully jumps to the main interface of the program
        Toast.makeText(LoginActivity.this, "Bind mobile number", Toast.LENGTH_SHORT).show();

        String userId = platform.getDb().getUserId();//Get user account
        String userName = platform.getDb().getUserName();//Get user name
        String userIcon = platform.getDb().getUserIcon();//Get user image
        String userGender = platform.getDb().getUserGender(); //Get user gender, m = male, f = female. If wechat does not set gender, return null by default
        //Now you can log in to your server or do what you want to do with the user information you get! "

Then how about the application of these accounts

shareSdk  

http://dashboard.mob.com/#!/index

qq developer account: This is the number after qq interconnection and qq developer Association

https://connect.qq.com/manage.html#/appcreate/ios

After association, you will be asked to create a new app with the id number of that app

As for the login of Weibo and QQ, it's just the difference of this sentence

 Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
Platform qq = ShareSDK.getPlatform(QQ.NAME);

The openId of the third party login is

  String userId = platform.getDb().getUserId();//Get user account

Other parameters need to be passed

        String userId = platform.getDb().getUserId();//Get user account
        String userName = platform.getDb().getUserName();//Get user name
        String userIcon = platform.getDb().getUserIcon();//Get user image
        String userGender = platform.getDb().getUserGender(); //Get user gender, m = male, f = female. If wechat does not set gender, return null by default
        //Now you can log in to your server or do what you want to do with the user information you get!

Now the application has the login function

Tags: SDK Gradle Mobile iOS

Posted on Sat, 09 Nov 2019 16:03:07 -0500 by lightkeepr