新浪通过API分享 实践

注:如果集成了百度的Frontia和SinaCoreSDK, 那么SSO会出现包冲突

https://github.com/sinaweibosdk/weibo_android_sdk/issues/56

要先获取API发送的权限

 

 

public class ShareToSinaActivity extends DFActivity {

  

    private WeiboAuthHandler weiboAuthHandler;



    private String shareTitle, shareContent, shareUrlLink, shareImageLink;

   

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);



        shareTitle = this.getIntent().getExtras().getString("shareTitle");

        shareContent = this.getIntent().getExtras().getString("shareContent");

        shareImageLink = this.getIntent().getExtras().getString("shareImageLink");

        shareUrlLink = this.getIntent().getExtras().getString("shareUrlLink");



    }



    public void sendShareMessageWithAPI() {

        // 获取当前已保存过的 Token

        Oauth2AccessToken mAccessToken = SinaAccessTokenKeeper.readAccessToken(this);

        if (mAccessToken != null && mAccessToken.isSessionValid()) {

            /*

              用于获取微博信息流等操作的API

             */

            StatusesAPI mStatusesAPI = new StatusesAPI(mActivity, AppConstants.KEY_SINA_WEIBO, mAccessToken);



            String strContent = shareTitle + " " + shareContent + " " + shareUrlLink + "(分享自@穿帮时尚)";

            mStatusesAPI.uploadUrlText(strContent, shareImageLink, null, null, null, mListener);

        } else {

            SinaAccessTokenKeeper.clear(mActivity);// 清理新浪Token

            weiboAuthHandler = new WeiboAuthHandler(mActivity);

            weiboAuthHandler.authorize(false);

        }

    }



 

    /**

     * 当 SSO 授权 Activity 退出时,该函数被调用。 当用户登录结束、注册结束时,调用该函数

     *

     * @see {@link android.app.Activity#onActivityResult}

     */

    @Override

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

        // requestCode标示请求的标示 resultCode表示有数据

        /**

         * 微博登录结果

         */

        weiboAuthHandler.authorizeCallBack(requestCode, resultCode, data);



    }

    

     /**

     * 微博 OpenAPI 回调接口。

     */

    private RequestListener mListener = new RequestListener() {

        @Override

        public void onComplete(String response) {

            if (!TextUtils.isEmpty(response)) {

                LogUtil.i(tag, response);

                if (response.startsWith("{\"statuses\"")) {

                    // 调用 StatusList#parse 解析字符串成微博列表对象

                    SinaStatusList statuses = SinaStatusList.parse(response);

                    if (statuses != null && statuses.total_number > 0) {

//                        AppUtils.showToast(mActivity, "获取微博信息流成功, 条数: " + statuses.statusList.size());

                    }

                } else if (response.startsWith("{\"created_at\"")) {

                    // 调用 Status#parse 解析字符串成微博对象

                    SinaStatus status = SinaStatus.parse(response);

                    AppUtils.showToast(mActivity, "分享成功");

                } else {

                    log("Sina Share " + response);

                }

            }

            finish();

        }



        @Override

        public void onWeiboException(WeiboException e) {

            LogUtil.e(tag, e.getMessage());

            SinaErrorInfo info = SinaErrorInfo.parse(e.getMessage());

            log("Sina Share " + info.toString());

            finish();

        }

    };

}

 

你可能感兴趣的:(api)