V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
hailong0707
V2EX  ›  程序员

ESSocialSDK:Android 社会化授权登录和分享工具

  •  2
     
  •   hailong0707 · 2015-12-15 10:56:31 +08:00 · 2734 次点击
    这是一个创建于 3047 天前的主题,其中的信息可能已经有所发展或是发生改变。

    之前发布过一篇 http://www.v2ex.com/t/238165 ,当时功能不是很完善,文档也不够清楚。
    当前发布正式版 0.2.0 ,所有功能经过测试。

    Github: https://github.com/ElbbbirdStudio/ESSocialSDK
    Build Status
    version
    LICENSE
    forks
    stars

    社交登录授权,分享 SDK

    支持微信、微博、 QQ 登录授权

    微信好友、微信朋友圈、微博、 QQ 好友、 QQ 空间分享以及系统默认分享

    说明

    每单个平台全部提供文档说明,内容较多,易混淆集成过程,所以,这里只提供一键登录,一键分享文档,使用默认 UI 。

    如果需要单个平台的详细集成文档,参考README_Detail.md

    <!-- more -->
    默认 UI 效果截图:

    一键登录
    一键分享

    Gradle

    compile 'com.elbbbird.android:socialsdk:0.2.0@aar'
    

    Debug 模式

    SocialSDK.setDebugMode(true); //默认 false
    

    项目配置

    • 配置微博后台回调地址

      SDK 的默认回调地址为http://www.sina.com,需要在微博后台配置,否则会提示回调地址错误。

      如果在SocialSDK.init()方法自定义了回调地址,需要在后台配置为相应地址。

    • WXEntryActivity

      创建包名:package_name.wxapi

      在该包名下创建类WXEntryActivity继承自WXCallbackActivity

    package com.encore.actionnow.wxapi;
    public class WXEntryActivity extends WXCallbackActivity {
    
    }
    
    • AndroidManifest.xml
    <activity
        android:name=".wxapi.WXEntryActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:exported="true"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />
    <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="tencentXXXXXXXXX" />
        </intent-filter>
    </activity>
    

    以上配置中的XXXXXXXXX换成 app_id.

    一键登录授权功能(微博,微信, QQ )

    授权结果回调

    SDK 使用了Otto作为事件库,用以组件通信。
    在调用SocialSDK.oauth()接口ActivityonCreate()方法内添加

    BusProvider.getInstance().register(this);
    

    在该ActivityonDestroy()方法添加

    @Override
    protected void onDestroy() {
        BusProvider.getInstance().unregister(this);
        super.onDestroy();
    }
    

    添加回调接口

    @Subscribe
    public void onOauthResult(SSOBusEvent event) {
        switch (event.getType()) {
            case SSOBusEvent.TYPE_GET_TOKEN:
                SocialToken token = event.getToken();
                Log.i(TAG, "onOauthResult#BusEvent.TYPE_GET_TOKEN " + token.toString());
                break;
            case SSOBusEvent.TYPE_GET_USER:
                SocialUser user = event.getUser();
                Log.i(TAG, "onOauthResult#BusEvent.TYPE_GET_USER " + user.toString());
                break;
            case SSOBusEvent.TYPE_FAILURE:
                Exception e = event.getException();
                Log.i(TAG, "onOauthResult#BusEvent.TYPE_FAILURE " + e.toString());
                break;
            case SSOBusEvent.TYPE_CANCEL:
                Log.i(TAG, "onOauthResult#BusEvent.TYPE_CANCEL");
                break;
        }
    }
    

    Oauth

    SocialSDK.init("wechat_app_id", "wechat_app_secret", "weibo_app_id", "qq_app_id");
    SocialSDK.oauth(context);
    

    Revoke

    SocialSDK.revoke(context);
    

    一键分享功能(微博,微信,朋友圈, QQ , QQ 空间)

    SDK 中SocialShareScene的定义

    /**
     * 社会化分享数据类
     */
    public class SocialShareScene implements Serializable {
    
        public static final int SHARE_TYPE_DEFAULT = 0;
        public static final int SHARE_TYPE_WEIBO = 1;
        public static final int SHARE_TYPE_WECHAT = 2;
        public static final int SHARE_TYPE_WECHAT_TIMELINE = 3;
        public static final int SHARE_TYPE_QQ = 4;
        public static final int SHARE_TYPE_QZONE = 5;
    
        /**
         * @param id        分享唯一标识符,可随意指定,会在分享结果 ShareBusEvent 中返回
         * @param appName   分享到 QQ 时需要指定,会在分享弹窗中显示该字段
         * @param type      分享类型
         * @param title     标题
         * @param desc      简短描述
         * @param thumbnail 缩略图网址
         * @param url       WEB 网址
         */
        public SocialShareScene(int id, String appName, int type, String title, String desc, String thumbnail, String url) {
            this.id = id;
            this.appName = appName;
            this.type = type;
            this.title = title;
            this.desc = desc;
            this.thumbnail = thumbnail;
            this.url = url;
        }
    
        public SocialShareScene(int id, String appName, String title, String desc, String thumbnail, String url) {
        ....
    }
    

    一键分享需要调用第二个构造函数, type 类型在 SDK 内部自动指定

    分享结果回调

    @Subscribe
    public void onShareResult(ShareBusEvent event) {
        switch (event.getType()) {
            case ShareBusEvent.TYPE_SUCCESS:
                Log.i(TAG, "onShareResult#ShareBusEvent.TYPE_SUCCESS " + event.getId());
                break;
            case ShareBusEvent.TYPE_FAILURE:
                Exception e = event.getException();
                Log.i(TAG, "onShareResult#ShareBusEvent.TYPE_FAILURE " + e.toString());
                break;
            case ShareBusEvent.TYPE_CANCEL:
                Log.i(TAG, "onShareResult#ShareBusEvent.TYPE_CANCEL");
                break;
        }
    }
    

    ShareTo

    SocialSDK.setDebugMode(true);
    SocialSDK.init("wechat_app_id", "weibo_app_id", "qq_app_id");
    SocialSDK.shareTo(context, scene);
    

    FAQ

    • 关于三个平台的账号

      微博应用程序注册完成后,需要在后台配置测试账号,包名,签名信息,然后开始测试;

      微信应用程序注册后,需要配置包名和签名,并提交审核通过,可以获得分享权限。 SSO 登录权限需要开发者认证。(保护费不到位,测试都不能做)

      QQ 需要在后台配置测试账号才能 SSO 登录。

    • 如果只需要 SDK 中 SSO 授权和分享功能其中之一,项目配置是否有删减?

      集成任何其中一个功能都需要做这些配置。

    • 是否需要配置权限?

      SDK 已经在 aar 中添加三个平台需要的权限,以下

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
    • ProGrard 代码混淆
    ##微信
    -keep class com.tencent.mm.sdk.** {*;}
    
    ##微博
    -keep public class com.sina.weibo.** {*;}
    -keep public class com.sina.sso.** {*;}
    
    ##otto
    -keepattributes *Annotation*
    -keepclassmembers class ** {
        @com.squareup.otto.Subscribe public *;
        @com.squareup.otto.Produce public *;
    }
    

    LICENSE

    Copyright 2015 The ESSocialSDK authors
    
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    
        http://www.apache.org/licenses/LICENSE-2.0
    
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
    
    3 条回复    2015-12-15 14:09:39 +08:00
    oott123
        1
    oott123  
       2015-12-15 12:22:01 +08:00
    在 Android 上最痛恨的事情之一莫过于内置分享组件(甚至都没有“更多”给你选)的 App 。
    不过社会化登录倒是还好……
    anthonyeef
        2
    anthonyeef  
       2015-12-15 12:32:39 +08:00
    好赞!
    hailong0707
        3
    hailong0707  
    OP
       2015-12-15 14:09:39 +08:00
    @oott123 之所以内置,是因为这样才能分享更好的效果, android 标准接口做不到
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3049 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 14:51 · PVG 22:51 · LAX 07:51 · JFK 10:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.