android ShareSDK集成第三方登录和分享

android ShareSDK集成第三方登录和分享

MobTech中注册账号,进入开发者平台中添加应用获取自己的AppKey和App Secret

在线引用配置文件
在根模块build.gradle中添加

buildscript {
    
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.2'
        // 注册MobSDK
        classpath "com.mob.sdk:MobSDK:2018.0319.1724"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

在APP build.gradle中添加 appKey 和 appSecret 中填写自己在Mob官网中添加应用获取到的key和secret
注意这里一定要换成自己的key或者Secret,如果是默认的分享或者登录授权的时候显示的是美的厨房

apply plugin: 'com.mob.sdk'
MobSDK {
    appKey "替换为mob官方申请的appkey"
    appSecret "替换为mob官方申请的appkey对应的appSecret"
    ShareSDK {
        loopShare true
        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"
            }
            Facebook {
                appKey "1412473428822331"
                appSecret "a42f4f3f867dc947b9ed6020c2e93558"
                callbackUri "https://mob.com"
            }
            Twitter {
                appKey "LRBM0H75rWrU9gNHvlEAA2aOy"
                appSecret "gbeWsZvA9ELJSdoBzJ5oLKX0TU09UOwrzdGfo9Tg7DjyGuMe8G"
                callbackUri "http://mob.com"
            }
            ShortMessage {}
            LinkedIn {
                appKey "ejo5ibkye3vo"
                appSecret "cC7B2jpxITqPLZ5M"
                callbackUri "http://www.sharesdk.cn"
            }
            Douyin {
                appKey "aw9ivykfjvi4hpwo"
                appSecret "42b4caa6bda60bd49f05f06d0a4956e1"
            }
            FacebookMessenger {
                appId "107704292745179"
            }
            WhatsApp {}
            Alipay {
                appId "2015072400185895"
            }
            Wework {
                appKey "wwa21eaecf93f0e3ba"
                appSecret "dW7e27P7Hc8NiYdRxnbTeOLgfI1ugR72e-PM8uusq2s"
                agentId "1000012"
                schema "wwautha21eaecf93f0e3ba000012"
            }
            TencentWeibo {
                appKey "801307650"
                appSecret "ae36f4ee3946e1cbb98d6965b0b2ff5c"
                callbackUri "http://www.sharesdk.cn"
            }
            WechatMoments {
                appId "wx4868b35061f87885"
                appSecret "64020361b8ec4c99936c0e3999a9f249"
            }
            WechatFavorite {
                appId "wx4868b35061f87885"
                appSecret "64020361b8ec4c99936c0e3999a9f249"
            }
        }
    }
}

之后再到需要分享登录的第三方网站中创建自己的应用获取相关配置信息
这里我就写个一个QQ的第三方登录

public class Fragment1 extends Fragment implements PlatformActionListener {
    private View view;
    private TextView textView;
    private Context context;

    private MyHandler myHandler;
    private class  MyHandler extends Handler{
        private WeakReference<Fragment1> weakReference;

        public MyHandler(Fragment1 fragment1) {
            weakReference=new WeakReference<Fragment1>(fragment1);
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.arg1){
                case 1:
                    HashMap<String, Object> hashMap= (HashMap<String, Object>) msg.obj;
                    hashMap.get("city");
                    for (String str:hashMap.keySet()){
                        hashMap.get(str);
                    }
                    break;
            }
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context=getActivity();
        view = inflater.inflate(R.layout.fragment_layout, container, false);
        initView();
        textView.setText("Fragment1");
        myHandler=new MyHandler(Fragment1.this);
        return view;
    }

    private void initView() {
        textView = view.findViewById(R.id.tv);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Platform qq = ShareSDK.getPlatform(QQ.NAME);
                qq.setPlatformActionListener(Fragment1.this);
                qq.SSOSetting(false);
// authorize(qq, 2);
                //如果授权就删除授权资料
                qq.showUser(null);//授权并获取用户信息
            }
        });
    }


    @Override
    public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
        super.onMultiWindowModeChanged(isInMultiWindowMode);

    }

    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
        Log.i("hashMap",hashMap.toString());
        Message message=new Message();
        message.obj=hashMap;
        message.arg1=1;
        myHandler.sendMessage(message);
    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        Log.i("失败",throwable.toString());

    }

    @Override
    public void onCancel(Platform platform, int i) {
        Log.i("取消",""+i);
    }
}

这是我QQ授权到的数据格式…这里可以参考 如果需要登录微信第三方可以更改成ShareSDK.getPlatform(Wechat.NAME);

I/hashMap: {is_yellow_vip=0, msg=, vip=0, nickname=**繁华 (枣仁), figureurl_qq=http://thirdqq.qlogo.cn/g?b=oidb&k=Ovld7icj5kwJVoa69hXJ0uQ&s=140&t=1555163536, figureurl_qq_1=http://thirdqq.qlogo.cn/g?b=oidb&k=Ovld7icj5kwJVoa69hXJ0uQ&s=40&t=1555163536, city=台州, constellation=, gender=女, figureurl_1=http://qzapp.qlogo.cn/qzapp/1109861925/71209411C28D112C9FECC79DE8C435BB/50, province=浙江, is_yellow_year_vip=0, figureurl_type=1, year=1992, yellow_vip_level=0, figureurl=http://qzapp.qlogo.cn/qzapp/1109861925/71209411C28D112C9FECC79DE8C435BB/30, figureurl_2=http://qzapp.qlogo.cn/qzapp/1109861925/71209411C28D112C9FECC79DE8C435BB/100, userTags=, is_lost=0, figureurl_qq_2=http://thirdqq.qlogo.cn/g?b=oidb&k=Ovld7icj5kwJVoa69hXJ0uQ&s=100&t=1555163536, level=0, ret=0}

点赞