Android Box API在初始OAuth后无法运行

时间:2015-06-29 20:53:10

标签: android oauth box-api boxapiv2

我正在使用Android的Box API。我正在使用Android Studio并在模拟器上进行测试。

我有一个用于启动OAuth请求的按钮,在进行身份验证后,我将返回到我的应用程序并显示另一个按钮以执行其他任务(我计划添加免费/已用/总空间)

我正在使用以下代码,但是一旦我返回我的应用程序,另一个按钮就不显示了,如果我点击我的初始按钮,则没有任何反应。

有人能指出我正确的方向吗?

感谢。

private void getUserIdUsingBox() {
    BoxConfig.CLIENT_ID = BoxController.BOX_CLIENT_ID;
    BoxConfig.CLIENT_SECRET = BoxController.BOX_SECRET;
    BoxSession session = new BoxSession(this);
    session.authenticate();

} //getUserIdsUsingBox

public void onLinkBox(View view) { getUserIdUsingBox(); }

private void invalidate() {
    StringBuilder msg = new StringBuilder("List of available controllers: ");
    for (Controller controller : mManager.getAvailableControllers()) {
        msg.append("\n" + controller.getClass().getSimpleName());
    }
    mText.setText(msg);

    List<LinkedAccount> linkedAccounts = mManager.getAvailableAccounts();
    for (LinkedAccount acct : linkedAccounts) {
        if (acct.getServiceName().equals(GoogleDriveController.NAME)) {
            mLinkGoogleDriveButton.setVisibility(View.GONE);
            mTestGoogleDriveButton.setVisibility(View.VISIBLE);
        }
        if (acct.getServiceName().equals(DropboxController.NAME)) {
            mLinkDropboxButton.setVisibility(View.GONE);
            mTestDropboxButton.setVisibility(View.VISIBLE);
        }

        if (acct.getServiceName().equals(BoxController.NAME))
        {
            mLinkBoxButton.setVisibility(View.GONE);
            mTestBoxButton.setVisibility(View.VISIBLE);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

正如greg所提到的 - 看看你的UI逻辑会很有帮助。但是这样的事情应该有效:

    final Button btnToAuthenticate = (Button) findViewById(R.id.auth_box_btn);
    final Button btnAfterAuthenticate = (Button) findViewById(R.id.box_action_btn);
    BoxConfig.CLIENT_ID = BoxController.BOX_CLIENT_ID;
    BoxConfig.CLIENT_SECRET = BoxController.BOX_SECRET;
    BoxSession session = new BoxSession(this);

    btnToAuthenticate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mSession.authenticate();
        }
    });
    mSession.setSessionAuthListener(new BoxAuthentication.AuthListener() {
        @Override
        public void onRefreshed(BoxAuthentication.BoxAuthenticationInfo boxAuthenticationInfo) {

        }

        @Override
        public void onAuthCreated(BoxAuthentication.BoxAuthenticationInfo boxAuthenticationInfo) {
            btnAfterAuthenticate.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAuthFailure(BoxAuthentication.BoxAuthenticationInfo boxAuthenticationInfo, Exception e) {

        }

        @Override
        public void onLoggedOut(BoxAuthentication.BoxAuthenticationInfo boxAuthenticationInfo, Exception e) {

        }
    });