本篇在基于之前封装的SocialSDK的项目上增加了新浪微博 SDK的登录授权和分享。介绍了新浪微博登录分享的接入和使用注意事项。
具体代码项目Github地址:https://github.com/tsy12321/SocialSDKAndroid
0 系列文章
系列一 Android SDK的二次封装和使用
系列二 源码解析
系列三 微信SDK接入
系列四 QQ SDK接入
系列五 新浪微博 SDK接入
1 官方文档
新浪微博官方SDK:
https://github.com/sinaweibosdk/weibo_android_sdk
API WIKI(授权成功后可以使用access_token去调用接口获取数据):
http://open.weibo.com/wiki/%E5%BE%AE%E5%8D%9AAPI
2 新浪微博SDK说明
和 微信和QQ SDK不同,新浪微博SDK包含2个,一个是weiboSDKCore_3.1.4.jar, 还有一个是所有的so库。
需要同时引用。引用so库的方法如下
将所有so文件统一放到项目的目录app/jniLibs中(和libs同级), 然后在gradle中加上
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***
网友评论