Android Digits什么都不返回

时间:2016-03-10 12:58:14

标签: java android twitter-oauth twitter-digits

为什么Android数字在数字验证后并不总是返回onSuccess或onFalure?它只是完成了DigitsActivity并且什么都不做,但有时它没关系,我收到onSuccess。

public void initForm(){
    Digits.getSessionManager().clearActiveSession();// http://stackoverflow.com/questions/31679089/fabric-digits-returns-a-null-object-of-phonenumber-after-otp-verification-on-and
    DigitsAuthConfig.Builder digitsAuthConfigBuilder = new DigitsAuthConfig.Builder()
            .withAuthCallBack(new AuthCallback() {
                @Override
                public void success(DigitsSession digitsSession, String s) {
                    Toast.makeText(LoginActivity.this, s, Toast.LENGTH_LONG).show();
                    TwitterAuthToken token = (TwitterAuthToken)digitsSession.getAuthToken();
                    DigitsOAuthSigning oauthSigning = new DigitsOAuthSigning(SapronApplication.conf(), token);
                    Map<String, String> authHeaders = oauthSigning.getOAuthEchoHeadersForVerifyCredentials();
                    requestToken(authHeaders.get("X-Verify-Credentials-Authorization"));

                }

                @Override
                public void failure(DigitsException e) {
                    Toast.makeText(LoginActivity.this, e.toString(), Toast.LENGTH_LONG).show();
                }
            })
            .withPhoneNumber("+7");


    Digits.authenticate(digitsAuthConfigBuilder.build());
}

3 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,而且phoneNumber也像this question一样返回null。我在使用自定义按钮时解决了问题。主要问题是没有正确清除数字会话,并且在每次点击时都在withAuthCallBack()中设置新的AuthCallback。解决我的问题的代码如下。希望它能帮助那些需要使用数字自定义按钮的人.....

findViewById(R.id.customDigitsButton).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            verifyMobileUsingDigits();
        }
    });

private AuthCallback authCallback;
public void verifyMobileUsingDigits(){
    logoutFabricDigits("verifyMobileUsingDigits");// manual deletion of all digit sessions, as only Digits.getSessionManager().clearActiveSession() did not helped me.....
    if(authCallback == null){
        authCallback = new AuthCallback() {
            @Override
            public void success(DigitsSession session, String phoneNumber) {
                Toast.makeText(getApplicationContext(), "Authentication successful for " + phoneNumber, Toast.LENGTH_LONG).show();
            }

            @Override
            public void failure(DigitsException exception) {
                Toast.makeText(getApplicationContext(), "Authentication failure", Toast.LENGTH_LONG).show();
            }
        };
    }
    DigitsAuthConfig.Builder digitsAuthConfigBuilder = new DigitsAuthConfig.Builder()
            .withAuthCallBack(authCallback)
            .withPhoneNumber("+880")
            .withEmailCollection(false)
            .withThemeResId(R.style.CustomDigitsTheme);
    Digits.authenticate(digitsAuthConfigBuilder.build());
}

public static void logoutFabricDigits(String TAG){
    try {
        Map<Long, DigitsSession> digitsSessionMap = Digits.getSessionManager().getSessionMap();
        if(digitsSessionMap != null){
            Set<Long> digitIdSet = new HashSet<>(digitsSessionMap.keySet());
            for(Long digitId : digitIdSet){
                DigitsSession digitsSession = digitsSessionMap.get(digitId);
                if(digitsSession != null){
                    Digits.getSessionManager().clearSession(digitsSession.getId());
                }
            }
        }
        Digits.getSessionManager().clearActiveSession();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

答案 1 :(得分:0)

好吧我不知道自定义按钮和自定义authcallback有什么问题。使用DigitsAuthButton解决了问题。

  digitsButton = (DigitsAuthButton) findViewById(R.id.auth_button);
   // digitsButton.setAuthTheme(R.style.AppTheme2);
    digitsButton.setCallback(new AuthCallback() {
        @Override
        public void success(DigitsSession digitsSession, String s) {
            new MaterialDialog.Builder(LoginActivity.this)
                    .progress(true, 0)
                    .show();
           TwitterAuthToken token = (TwitterAuthToken)digitsSession.getAuthToken();
            DigitsOAuthSigning oauthSigning = new DigitsOAuthSigning(SapronApplication.conf(), token);
            Map<String, String> authHeaders = oauthSigning.getOAuthEchoHeadersForVerifyCredentials();
            requestToken(authHeaders.get("X-Verify-Credentials-Authorization"));
        }

        @Override
        public void failure(DigitsException e) {

        }
    });

答案 2 :(得分:0)

正如@rallat在此评论https://github.com/twitter/digits-android/issues/6

他们将authcallback更改为弱引用,以避免内存泄漏。

只需在您的上下文中创建回调变量,f.e:

AuthCallback authCallback = new AuthCallback() {
    @Override
    public void success(DigitsSession session, String phoneNumber) {
        // Do something with the session
        Log.d(TAG, "success: " + phoneNumber);
    }

    @Override
    public void failure(DigitsException exception) {
        // Do something on failure
        Log.d(TAG, "failure: ");
        //show error or somthing
    }
};

并将其传递给您的构建者:

digitsButton.setCallback(authCallback);