社會化登陸分享-新浪微博 SDK接入

本篇在基於以前封裝的SocialSDK的項目上增長了新浪微博 SDK的登陸受權和分享。介紹了新浪微博登陸分享的接入和使用注意事項。java

具體代碼項目Github地址:github.com/tsy12321/So…android

0 系列文章

系列一 Android SDK的二次封裝和使用
系列二 源碼解析
系列三 微信SDK接入
系列四 QQ SDK接入
系列五 新浪微博 SDK接入git

1 官方文檔

新浪微博官方SDK:github

github.com/sinaweibosd…安全

API WIKI(受權成功後可使用access_token去調用接口獲取數據):bash

open.weibo.com/wiki/%E5%BE…微信

2 新浪微博SDK說明

和 微信和QQ SDK不一樣,新浪微博SDK包含2個,一個是weiboSDKCore_3.1.4.jar, 還有一個是全部的so庫。app

須要同時引用。引用so庫的方法以下框架

將全部so文件統一放到項目的目錄app/jniLibs中(和libs同級), 而後在gradle中加上ide

android {

    ...

    //引入微博的全部so庫
    sourceSets {
        main {
            jniLibs.srcDirs = ['jniLibs']
        }
    }
}複製代碼

3 集成登陸受權

在以前的框架裏建立一個SinaWBHandler,而且在PlatformType、PlatformConfig增長新浪微博的配置類型。

而後在SinaWBHandler先初始化mAuthInfo:

this.mAuthInfo = new AuthInfo(mContext, mConfig.appKey, REDIRECT_URL, SCOPE);複製代碼

受權登陸時調用:

mSsoHandler.authorize(new WeiboAuthListener() {
            @Override
            public void onComplete(Bundle bundle) {
                // 從 Bundle 中解析 Token
                Oauth2AccessToken accessToken = Oauth2AccessToken.parseAccessToken(bundle);
                if(accessToken.isSessionValid()) {
                    Map<String, String> map = new HashMap<String, String>();
                    map.put("uid", accessToken.getUid());
                    map.put("access_token", accessToken.getToken());
                    map.put("refresh_token", accessToken.getRefreshToken());
                    map.put("expire_time", "" + accessToken.getExpiresTime());

                    mAuthListener.onComplete(mConfig.getName(), map);
                } else {
                    String errmsg = "errmsg=accessToken is not SessionValid";
                    LogUtils.e(errmsg);
                    mAuthListener.onError(mConfig.getName(), errmsg);
                }
            }

            @Override
            public void onWeiboException(WeiboException e) {
                String errmsg = "errmsg=" + e.getMessage();
                LogUtils.e(errmsg);
                mAuthListener.onError(mConfig.getName(), errmsg);
            }

            @Override
            public void onCancel() {
                mAuthListener.onCancel(mConfig.getName());
            }
        });複製代碼

最後返回access_token, uid等數據給使用sdk用戶,用戶能夠保存或者直接使用access_token去調用新浪微博的API獲取各類新浪微博數據。

回調處理,直接在SinaWBHandler中實現onActivityResult

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    mSsoHandler.authorizeCallBack(requestCode, resultCode, data);
}複製代碼

3 分享

新浪微博的分享有幾個注意點。在新浪微博SDK接入說明中,

發起分享的Activity的AndroidManifest中要添加如下代碼

<activity android:name="com.tsy.girl.MainActivity"> <!--發起分享的Activity-->

    <!--增長的代碼-->
    <intent-filter>
        <action android:name="com.sina.weibo.sdk.action.ACTION_SDK_REQ_ACTIVITY" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>複製代碼

發起分享的Activity要實現IWeiboHandler.Response接口,而且重寫onNewIntent,在onNewIntent中要調用新浪微博的SDK方法,才能正確回調。

因而咱們提供封裝的SDK要求用戶在這幾個地方主動調用SinaWBHandler的方法,具體的處理在SinaWBHandler中進行處理,詳細能夠見系列一文章的新浪微博接入說明。

!!在這裏注意一點,實現IWeiboHandler.Response的必須是Activity方法,新浪微博SDK中會判斷若是instantof Activity, 在這新浪微博的SDK沒有進行說明, 若是不是Activity會收不到分享回調

具體的分享代碼能夠直接參見新浪微博SDK的說明,5種媒介均可以實現,集成的代碼以下:

@Override
public void share(Activity activity, IShareMedia shareMedia, ShareListener shareListener) {
    this.mActivity = activity;
    this.mShareListener = shareListener;

    this.mSsoHandler = new SsoHandler(mActivity, mAuthInfo);

    WeiboMultiMessage weiboMessage = new WeiboMultiMessage();

    if(shareMedia instanceof ShareWebMedia) {       //網頁分享
        ShareWebMedia shareWebMedia = (ShareWebMedia) shareMedia;

        WebpageObject mediaObject = new WebpageObject();
        mediaObject.identify = Utility.generateGUID();
        mediaObject.title = shareWebMedia.getTitle();
        mediaObject.description = shareWebMedia.getDescription();
        mediaObject.setThumbImage(shareWebMedia.getThumb());
        mediaObject.actionUrl = shareWebMedia.getWebPageUrl();

        weiboMessage.mediaObject = mediaObject;
    } else if(shareMedia instanceof ShareTextMedia) {   //文字分享
        ShareTextMedia shareTextMedia = (ShareTextMedia) shareMedia;

        TextObject textObject = new TextObject();
        textObject.text = shareTextMedia.getText();

        weiboMessage.textObject = textObject;
    } else if(shareMedia instanceof ShareImageMedia) {  //圖片分享
        ShareImageMedia shareImageMedia = (ShareImageMedia) shareMedia;

        ImageObject imageObject = new ImageObject();
        imageObject.setImageObject(shareImageMedia.getImage());

        weiboMessage.imageObject = imageObject;
    } else if (shareMedia instanceof ShareMusicMedia) {  //音樂分享
        ShareMusicMedia shareMusicMedia = (ShareMusicMedia) shareMedia;

        MusicObject musicObject = new MusicObject();
        musicObject.identify = Utility.generateGUID();
        musicObject.title = shareMusicMedia.getTitle();
        musicObject.description = shareMusicMedia.getDescription();

        musicObject.setThumbImage(shareMusicMedia.getThumb());
        musicObject.actionUrl = shareMusicMedia.getMusicUrl();
        musicObject.dataUrl = shareMusicMedia.getMusicUrl();
        musicObject.dataHdUrl = shareMusicMedia.getMusicUrl();
        musicObject.duration = 10;
        musicObject.defaultText = "music 默認文案";

        weiboMessage.mediaObject = musicObject;
    } else if(shareMedia instanceof ShareVideoMedia) {      //視頻分享
        ShareVideoMedia shareVideoMedia = (ShareVideoMedia) shareMedia;

        VideoObject videoObject = new VideoObject();
        videoObject.identify = Utility.generateGUID();
        videoObject.title = shareVideoMedia.getTitle();
        videoObject.description = shareVideoMedia.getDescription();

        videoObject.setThumbImage(shareVideoMedia.getThumb());
        videoObject.actionUrl = shareVideoMedia.getVideoUrl();
        videoObject.dataUrl = shareVideoMedia.getVideoUrl();
        videoObject.dataHdUrl = shareVideoMedia.getVideoUrl();
        videoObject.duration = 10;
        videoObject.defaultText = "Vedio 默認文案";

        weiboMessage.mediaObject = videoObject;
    } else {
        if(this.mShareListener != null) {
            this.mShareListener.onError(this.mConfig.getName(), "shareMedia error");
        }
        return ;
    }

    SendMultiMessageToWeiboRequest request = new SendMultiMessageToWeiboRequest();
    request.transaction = String.valueOf(System.currentTimeMillis()); request.multiMessage = weiboMessage;
    mWeiboShareAPI.sendRequest(mActivity, request);
}複製代碼

回調處理:

public void onNewIntent(Intent intent, IWeiboHandler.Response response) {
    mWeiboShareAPI.handleWeiboResponse(intent, response);
}

public void onResponse(BaseResponse baseResponse) {
    if(baseResponse!= null){
        switch (baseResponse.errCode) {
            case WBConstants.ErrorCode.ERR_OK:
                if(this.mShareListener != null) {
                    this.mShareListener.onComplete(this.mConfig.getName());
                }
                break;
            case WBConstants.ErrorCode.ERR_CANCEL:
                if(this.mShareListener != null) {
                    this.mShareListener.onCancel(this.mConfig.getName());
                }
                break;
            case WBConstants.ErrorCode.ERR_FAIL:
                if(this.mShareListener != null) {
                    this.mShareListener.onError(this.mConfig.getName(), baseResponse.errMsg);
                }
                break;
        }
    }
}複製代碼

結尾

以上即實現了新浪微博的接入。至此,該社會化分享組件完成了微信、QQ、新浪微博的受權登陸和分享的集成。相對於Umeng等第三方來講,沒有將app_secret放到客戶端,獲取到access_token後直接返回,讓服務端去處理。增長了安全性。也方便之後擴展定製。(以前用UMeng的社會化組件必須傳入app_secret)

PS:以前還集成過FaceBook、Twitter、Google,糾結要不要加進去。感受國內基本上不會用到。。。歡迎來Github提issue和fork,star

相關文章
相關標籤/搜索