mobileresearch / weibo_android_sdk

sina weibo android sdk
644 stars 764 forks source link

微博授权没调用到回调 #46

Closed zouguangxian closed 10 years ago

zouguangxian commented 11 years ago

基于你们官方的demo做测试,发现不能调用到回调。 反编译SDK后,发现应该是SDK的bug。 // 获取token测试button findViewById(R.id.auth).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mWeibo.anthorize(MainActivity.this, new AuthDialogListener()); } });

public void anthorize(final Context ct, final WeiboAuthListener mlistener) {
    this.mlistener = mlistener;
    this.ct = ct;
    Weibo.mPackagename = this.ct.getApplicationContext().getPackageName();
    Weibo.isWifi = Utility.isWifi(this.ct);
    this.startAuthDialog(ct, mlistener, 0);
}

在anthorize里,准备好参数后,调用startAuthDialog打开WeiboDialog,并设置回调函数
public void startAuthDialog(final Context context, final WeiboAuthListener weiboAuthListener, final int n) {
    final WeiboParameters weiboParameters = new WeiboParameters();
    CookieSyncManager.createInstance(context);
    this.startDialog(context, weiboParameters, new WeiboAuthListener() {
        public void onCancel() {
            weiboAuthListener.onCancel();
        }

        public void onComplete(final Bundle bundle) {
            CookieSyncManager.getInstance().sync();
            final String string = bundle.getString("code");
            if (1 == n) {
        Weibo.FtchAccessToken(string);
            }
            else if (n == 0) {
                Weibo.KeepAccessToken(bundle, weiboAuthListener);
                return;
            }
        }

        public void onError(final WeiboDialogError weiboDialogError) {
            weiboAuthListener.onError(weiboDialogError);
        }

        public void onWeiboException(final WeiboException ex) {
            weiboAuthListener.onWeiboException(ex);
        }
    });
}

而KeepAccessToken的实现是:
private void KeepAccessToken(final Bundle bundle, final WeiboAuthListener weiboAuthListener) {
    if (bundle.getString("code") == null) {
        final String string = bundle.getString("access_token");
        final String string2 = bundle.getString("expires_in");
        if (TextUtils.isEmpty(string) || TextUtils.isEmpty(string2)) {
            weiboAuthListener.onWeiboException(new WeiboException("\u6388\u6743\u5931\u8d25\uff01"));
            return;
        }
        final Oauth2AccessToken oauth2AccessToken = new Oauth2AccessToken(string, string2);
        if (oauth2AccessToken.isSessionValid()) {
            AccessTokenKeeper.keepAccessToken(this.ct, oauth2AccessToken);
            Toast.makeText(this.ct, this.ct.getString(R.string.auth_succ), 0).show();
            weiboAuthListener.onComplete(bundle);
            return;
        }
    }
}

当用户发起授权时,微博服务端返回redirect并在query_string中带了code信息。 当n==0时,调用KeepAccessToken,因为bundle.getString("code")不为null,因此直接返回了,即授权在本地没做任何处理。 即使将n设置为1,也不会触发传入的listener。

woyaowenzi commented 11 years ago

是不是没有调用mSsoHandler.authorizeCallBack(requestCode, resultCode, data);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // SSO 授权回调
    // 重要:发起 SSO 登陆的Activity必须重写onActivityResult
    if (mSsoHandler != null) {
        mSsoHandler.authorizeCallBack(requestCode, resultCode, data);
    }
}
denny37 commented 11 years ago

onActivityResult 都没有调用,重写有什么用

mobileresearch commented 11 years ago

请更新最新版本的代码,该问题已修正。

onActivityResult 都没有调用,重写有什么用。

mobileresearch commented 10 years ago

该问题已在V2.4.0中解决了

Vurtex commented 10 years ago
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    /**
     * 下面两个注释掉的代码,仅当sdk支持sso时有效,
     */
    if (mSsoHandler != null) {
        mSsoHandler.authorizeCallBack(requestCode, resultCode, data);
    }
}
Vurtex commented 10 years ago

虽然可以解决,但还是更新SDK吧.