Java Code Examples for com.sina.weibo.sdk.auth.Oauth2AccessToken#isSessionValid()

The following examples show how to use com.sina.weibo.sdk.auth.Oauth2AccessToken#isSessionValid() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseDialog.java    From AssistantBySDK with Apache License 2.0 6 votes vote down vote up
@Override
public void onComplete(Bundle values) {
    Log.i(TAG, "AuthListener.onComplete");
    Oauth2AccessToken accessToken = Oauth2AccessToken.parseAccessToken(values);
    if (accessToken != null && accessToken.isSessionValid()) {
        //UsersAPI userAPI=new UsersAPI(accessToken);
        //userAPI.show(Long.parseLong(accessToken.getUid()), userListener);

        String date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(
                new java.util.Date(accessToken.getExpiresTime()));
        System.out.println("uid=" + accessToken.getUid());
        System.out.println("access_token=" + accessToken.getToken());
        System.out.println("expiresTime=" + date);
        AccessTokenKeeper.writeAccessToken(mContext, accessToken);
        shareToWeibo();
    }
}
 
Example 2
Source File: Login.java    From LoginSharePay with Apache License 2.0 6 votes vote down vote up
@Override
public void onSuccess(Oauth2AccessToken oauth2AccessToken) {
    if (oauth2AccessToken.isSessionValid()) {
        Map<String, String> body = new HashMap<>();
        body.put("uid", oauth2AccessToken.getUid());
        body.put("access_token", oauth2AccessToken.getToken());
        new LoginShareHttp(Config.WEIBO_USERINFO_URL, body) {
            @Override
            public void onResult(int code, String result) {
                if (listener == null) {
                    return;
                }
                if (code == 0) {
                    listener.onLoginSuccess(Type.Weibo, result);
                } else {
                    listener.onLoginError(Type.Weibo, code);
                }
            }
        };
    }
}
 
Example 3
Source File: SinaShareHandler.java    From BiliShare with Apache License 2.0 6 votes vote down vote up
@Override
public void onSuccess(Oauth2AccessToken oauth2AccessToken) {
    Log.d(TAG, "auth success");
    mSsoHandler = null;
    if (oauth2AccessToken.isSessionValid()) {
        AccessTokenKeeper.writeAccessToken(getContext(), oauth2AccessToken);
        if (mWeiboMessage != null) {
            allInOneShare(mWeiboMessage);
        }
        return;
    }

    SocializeListeners.ShareListener listener = getShareListener();
    if (listener == null) {
        return;
    }

    listener.onError(SocializeMedia.SINA, BiliShareStatusCode.ST_CODE_SHARE_ERROR_AUTH_FAILED, new ShareException("无效的token"));
}
 
Example 4
Source File: BaseHandler.java    From LoginSharePay with Apache License 2.0 5 votes vote down vote up
public void authorizeCallBack(int requestCode, int resultCode, Intent data) {
    if ('胍' == requestCode) {
        if (resultCode == -1) {
            if (!SecurityHelper.checkResponseAppLegal(this.mAuthFragment.getContext(), WeiboAppManager.getInstance(this.mAuthFragment.getContext()).getWbAppInfo(), data)) {
                this.authListener.onFailure(new WbConnectErrorMessage("your install weibo app is counterfeit", "8001"));
                return;
            }

            String error = Utility.safeString(data.getStringExtra("error"));
            String error_type = Utility.safeString(data.getStringExtra("error_type"));
            String error_description = Utility.safeString(data.getStringExtra("error_description"));
            LogUtil.d("WBAgent", "error: " + error + ", error_type: " + error_type + ", error_description: " + error_description);
            if (TextUtils.isEmpty(error) && TextUtils.isEmpty(error_type) && TextUtils.isEmpty(error_description)) {
                Bundle bundle = data.getExtras();
                Oauth2AccessToken accessToken = Oauth2AccessToken.parseAccessToken(bundle);
                if (accessToken != null && accessToken.isSessionValid()) {
                    LogUtil.d("WBAgent", "Login Success! " + accessToken.toString());
                    AccessTokenKeeper.writeAccessToken(this.mAuthFragment.getContext(), accessToken);
                    this.authListener.onSuccess(accessToken);
                }
            } else if (!"access_denied".equals(error) && !"OAuthAccessDeniedException".equals(error)) {
                LogUtil.d("WBAgent", "Login failed: " + error);
                this.authListener.onFailure(new WbConnectErrorMessage(error_type, error_description));
            } else {
                LogUtil.d("WBAgent", "Login canceled by user.");
                this.authListener.cancel();
            }
        } else if (resultCode == 0) {
            if (data != null) {
                this.authListener.cancel();
            } else {
                this.authListener.cancel();
            }
        }
    }

}
 
Example 5
Source File: WBHelper.java    From SocialHelper with Apache License 2.0 5 votes vote down vote up
private void initLoginListener() {
    wbAuthCallback = new WbAuthListener() {
        @Override
        public void onSuccess(Oauth2AccessToken oauth2AccessToken) {
            if (oauth2AccessToken.isSessionValid()) {
                loginResult = oauth2AccessToken;
                AccessTokenKeeper.writeAccessToken(activity, oauth2AccessToken);
                getUserInfo(oauth2AccessToken);
            } else {
                handler.sendEmptyMessage(GET_INFO_ERROR);
            }
        }

        @Override
        public void cancel() {
            if (loginCallback != null && activity != null) {
                loginCallback.socialError(activity.getString(R.string.social_cancel));
            }
        }

        @Override
        public void onFailure(WbConnectErrorMessage error) {
            if (loginCallback != null) {
                loginCallback.socialError(error.getErrorMessage());
            }
        }
    };
}
 
Example 6
Source File: WbLoginHelper.java    From SocialSdkLibrary with Apache License 2.0 5 votes vote down vote up
public void justAuth(final Activity activity, final WbAuthListener listener) {
    Oauth2AccessToken token = AccessToken.getToken(activity, mLoginTarget, Oauth2AccessToken.class);
    if (token != null && token.isSessionValid() && token.getExpiresTime() > System.currentTimeMillis()) {
        listener.onSuccess(token);
    } else {
        AccessToken.clearToken(activity, Target.LOGIN_WB);
        mSsoHandler.authorize(new WbAuthListener() {
            @Override
            public void onSuccess(Oauth2AccessToken oauth2AccessToken) {
                oauth2AccessToken.setBundle(null);
                SocialUtil.json("test", oauth2AccessToken.toString());
                AccessToken.saveToken(activity, mLoginTarget, oauth2AccessToken);
                listener.onSuccess(oauth2AccessToken);
            }

            @Override
            public void cancel() {
                listener.cancel();
            }

            @Override
            public void onFailure(WbConnectErrorMessage wbConnectErrorMessage) {
                listener.onFailure(wbConnectErrorMessage);
            }
        });
    }
}
 
Example 7
Source File: WeiboAuthManager.java    From sharesdk with Apache License 2.0 5 votes vote down vote up
@Override
public void onSuccess(Oauth2AccessToken accessToken) {
    if (accessToken != null && accessToken.isSessionValid()) {
        AccessTokenKeeper.writeAccessToken(mContext, accessToken);
        userAPI = new UsersAPI(mContext, mSinaAppKey, accessToken);
        userAPI.show(this);
    }
}
 
Example 8
Source File: SharePopWindow.java    From QiQuYing with Apache License 2.0 5 votes vote down vote up
/**
 * 分享到新浪微博
 */
private void share2SinaWeibo() {
	Oauth2AccessToken accessToken = AccessTokenKeeper.readAccessToken(context);
	if(accessToken != null && accessToken.isSessionValid()) {
		initShareWeiboAPI();
        share2weibo();
	} else {   //需要授权登录
		initSinaWeiBo();
		initShareWeiboAPI();
		mSsoHandler.authorize(new AuthListener());   //授权后再分享
	}
}
 
Example 9
Source File: SsoHandler.java    From letv with Apache License 2.0 4 votes vote down vote up
public void authorizeCallBack(int requestCode, int resultCode, Intent data) {
    LogUtil.d(TAG, "requestCode: " + requestCode + ", resultCode: " + resultCode + ", data: " + data);
    String error;
    Bundle bundle;
    Oauth2AccessToken accessToken;
    if (requestCode == this.mSSOAuthRequestCode) {
        if (resultCode == -1) {
            if (SecurityHelper.checkResponseAppLegal(this.mAuthActivity, this.mWeiboInfo, data)) {
                error = data.getStringExtra(NativeProtocol.BRIDGE_ARG_ERROR_BUNDLE);
                if (error == null) {
                    error = data.getStringExtra(NativeProtocol.BRIDGE_ARG_ERROR_TYPE);
                }
                if (error == null) {
                    bundle = data.getExtras();
                    accessToken = Oauth2AccessToken.parseAccessToken(bundle);
                    if (accessToken == null || !accessToken.isSessionValid()) {
                        LogUtil.d(TAG, "Failed to receive access token by SSO");
                        this.mWebAuthHandler.anthorize(this.mAuthListener);
                        return;
                    }
                    LogUtil.d(TAG, "Login Success! " + accessToken.toString());
                    this.mAuthListener.onComplete(bundle);
                } else if (error.equals("access_denied") || error.equals("OAuthAccessDeniedException")) {
                    LogUtil.d(TAG, "Login canceled by user.");
                    this.mAuthListener.onCancel();
                } else {
                    String description = data.getStringExtra(NativeProtocol.BRIDGE_ARG_ERROR_DESCRIPTION);
                    if (description != null) {
                        error = new StringBuilder(String.valueOf(error)).append(NetworkUtils.DELIMITER_COLON).append(description).toString();
                    }
                    LogUtil.d(TAG, "Login failed: " + error);
                    this.mAuthListener.onWeiboException(new WeiboDialogException(error, resultCode, description));
                }
            }
        } else if (resultCode != 0) {
        } else {
            if (data != null) {
                LogUtil.d(TAG, "Login failed: " + data.getStringExtra(NativeProtocol.BRIDGE_ARG_ERROR_BUNDLE));
                this.mAuthListener.onWeiboException(new WeiboDialogException(data.getStringExtra(NativeProtocol.BRIDGE_ARG_ERROR_BUNDLE), data.getIntExtra(NativeProtocol.BRIDGE_ARG_ERROR_CODE, -1), data.getStringExtra("failing_url")));
                return;
            }
            LogUtil.d(TAG, "Login canceled by user.");
            this.mAuthListener.onCancel();
        }
    } else if (requestCode != REQUEST_CODE_MOBILE_REGISTER) {
    } else {
        if (resultCode == -1) {
            bundle = data.getExtras();
            accessToken = Oauth2AccessToken.parseAccessToken(bundle);
            if (accessToken != null && accessToken.isSessionValid()) {
                LogUtil.d(TAG, "Login Success! " + accessToken.toString());
                this.mAuthListener.onComplete(bundle);
            }
        } else if (resultCode != 0) {
        } else {
            if (data != null) {
                LogUtil.d(TAG, "Login failed: " + data.getStringExtra(NativeProtocol.BRIDGE_ARG_ERROR_BUNDLE));
                error = data.getStringExtra(NativeProtocol.BRIDGE_ARG_ERROR_BUNDLE);
                if (error == null) {
                    error = data.getStringExtra(NativeProtocol.BRIDGE_ARG_ERROR_TYPE);
                }
                if (error != null) {
                    this.mAuthListener.onWeiboException(new WeiboDialogException(data.getStringExtra(NativeProtocol.BRIDGE_ARG_ERROR_BUNDLE), data.getIntExtra(NativeProtocol.BRIDGE_ARG_ERROR_CODE, -1), data.getStringExtra(NativeProtocol.BRIDGE_ARG_ERROR_DESCRIPTION)));
                    return;
                }
                return;
            }
            LogUtil.d(TAG, "Login canceled by user.");
            this.mAuthListener.onCancel();
        }
    }
}