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}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章