This paper added login authorization and sharing of Sina Weibo SDK to the project based on the SocialSDK encapsulated previously. This paper introduces the matters needing attention in the access and use of Sina Weibo blog sharing.

Specific code project Github address: github.com/tsy12321/So…

Series of articles

Series one Secondary encapsulation and use of Android SDK Series two source code analysis series three wechat SDK access series four QQ SDK access series five Sina Weibo SDK access

1 Official Documents

Official SDK of Sina Weibo:

Github.com/sinaweibosd…

API WIKI(after successful authorization, you can use access_token to call the interface for data) :

Open.weibo.com/wiki/%E5%BE…

2 Sina Weibo SDK description

Different from wechat and QQ SDK, Sina Weibo SDK contains two, one is WeibosdkCore_3.1.4. jar, and one is all so library.

It needs to be referenced simultaneously. The so library is referenced as follows

Place all so files in the project directory app/jniLibs (the same as libs) and add them to Gradle

android { ... // Add all the so libraries to microbloggingsourceSets {
        main {
            jniLibs.srcDirs = ['jniLibs']}}}Copy the code

3 Integrated login authorization

Create a SinaWBHandler in the previous framework and add the configuration type of Sina Weibo in PlatformType and PlatformConfig.

Then initialize mAuthInfo in SinaWBHandler:

this.mAuthInfo = new AuthInfo(mContext, mConfig.appKey, REDIRECT_URL, SCOPE);Copy the code

Call when authorizing login:

 mSsoHandler.authorize(new WeiboAuthListener() {
            @Override
            public void onComplete(Bundle bundle) {
                // Resolve tokens from the Bundle
                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(a) { mAuthListener.onCancel(mConfig.getName()); }});Copy the code

Finally, access_token, UID and other data are returned to users using SDK. Users can save or directly use access_token to call THE API of Sina Weibo to obtain various Sina Weibo data.

Callback handling, which implements onActivityResult directly in SinaWBHandler

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    mSsoHandler.authorizeCallBack(requestCode, resultCode, data);
}Copy the code

3 share

There are several caveats to sina Weibo’s sharing. In the SDK access instructions of Sina Weibo,

Add the following code to the AndroidManifest of the Activity that initiates the share

<activity android:name="com.tsy.girl.MainActivity"> <! -- Start the sharing Activity--> <! - increase the code - > < intent - filter > < action android: name = "com. Sina) weibo) SDK. Action. ACTION_SDK_REQ_ACTIVITY" / > < category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>Copy the code

The Activity initiated by sharing should implement iweiboHandle. Response interface and rewrite onNewIntent. In onNewIntent, the SDK method of Sina Weibo should be called to call back correctly.

Therefore, we provide packaged SDK requiring users to actively call SinaWBHandler methods in these places, and the specific processing will be handled in SinaWBHandler. For details, please refer to the Sina Weibo access instructions in the first series of articles.

!!!!! Note here, the implementation of iweiboHandler. Response must be the Activity method, Sina Weibo SDK will determine if instantof Activity, in this Sina Weibo SDK does not specify, if not the Activity will not receive the share callback

Specific sharing codes can be directly refer to the explanation of Sina Weibo SDK, which can be realized in all 5 media. The integrated codes are as follows:

@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) {       // Web sharing
        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) {   // Text sharing
        ShareTextMedia shareTextMedia = (ShareTextMedia) shareMedia;

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

        weiboMessage.textObject = textObject;
    } else if(shareMedia instanceof ShareImageMedia) {  // Image sharing
        ShareImageMedia shareImageMedia = (ShareImageMedia) shareMedia;

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

        weiboMessage.imageObject = imageObject;
    } else if (shareMedia instanceof ShareMusicMedia) {  // Music sharing
        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 Default copy";

        weiboMessage.mediaObject = musicObject;
    } else if(shareMedia instanceof ShareVideoMedia) {      // Video sharing
        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 = "Default Vedio copywriting";

        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);
}Copy the code

Callback processing:

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; }}}Copy the code

At the end

The above is the realization of sina Weibo access. So far, the social sharing component has completed the integration of authorized login and sharing of wechat, QQ and Sina Weibo. Compared with Umeng and other third parties, app_secret is not put on the client side, but returned directly after obtaining the Access_token and let the server deal with it. Increased security. It is also convenient to expand customization later. (Previous UMeng social components must be passed app_secret)

PS: I have also integrated FaceBook, Twitter and Google before, but I am struggling to add them. I feel like it’s basically not used in China… Welcome to Github for issue and fork, star