在 Android 上使用 Microsoft 进行身份验证

时间:2021-03-08 01:04:32

标签: java azure android-studio firebase-authentication

我在 Android 上登录 Microsoft 时遇到问题。大约一个月前,身份验证工作正常,从那时起没有进行任何更改,单击我的按钮时似乎崩溃了(我得到的只是黑屏)。

我尝试输入日志消息以查看是否可以捕获错误,但似乎在调用我的函数 signInToMicrosoft() 时崩溃了。

有谁知道可能的问题是什么?这是我的功能:

编辑:

我的函数能够显示来自 pendingResultTask 的消息,但随后立即崩溃并显示黑屏。看起来问题可能出在 .startActivityForSignInWithProvider 上: Photo

    private void signInToMicrosoft(){
        FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();

        OAuthProvider.Builder provider = OAuthProvider.newBuilder("microsoft.com");
        Log.d(TAG, "signing in with microsoft...");

        Task<AuthResult> pendingResultTask = firebaseAuth.getPendingAuthResult();
        if (pendingResultTask != null) {
        // There's something already here! Finish the sign-in for your user.
        pendingResultTask
            .addOnSuccessListener(
                new OnSuccessListener<AuthResult>() {
                    @Override
                    public void onSuccess(AuthResult authResult) {
                        // User is signed in.
                        // IdP data available in
                        // authResult.getAdditionalUserInfo().getProfile();
                        // The OAuth access token can also be retrieved:
                        // authResult.getCredential()).getAccessToken();
                        // The OAuth ID token can also be retrieved:
                        // authResult.getCredential()).getIdToken();
                    }
                })
            .addOnFailureListener(
                new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                    // Handle failure.
                    statusTextView.setText("Failed to load on pending result:");
                    }
                });
        } else {
        // There's no pending result so you need to start the sign-in flow.
        // See below.
        statusTextView.setText("No pending tasks... starting sign-in flow");
        }
        firebaseAuth
            .startActivityForSignInWithProvider(/* activity= */ this, provider.build())
            .addOnSuccessListener(
                new OnSuccessListener<AuthResult>() {
                @Override
                public void onSuccess(AuthResult authResult) {
                    // User is signed in.
                    // IdP data available in
                    Map<String,Object> mapProfile = authResult.getAdditionalUserInfo().getProfile();
                    Object displayName = mapProfile.get("displayName");

                    statusTextView.setText("Hello, " + displayName);
                    logger.info("Hello, " + displayName);
                    signInButton.setVisibility(signInButton.INVISIBLE);
                    microsoftSignInButton.setVisibility(microsoftSignInButton.INVISIBLE);
                    microsoftSignOutButton.setVisibility(microsoftSignOutButton.VISIBLE);
                    forwardButton.setVisibility(forwardButton.VISIBLE);
                    openActivity2();
                    }
                })
            .addOnFailureListener(
                new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    // Handle failure.
                    statusTextView.setText("ERROR on Microsoft sign-in: " + e);
                    logger.info("ERROR on Microsoft sign-in: " + e);
                    }
                });
        // The user is already signed-in.
        FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();

        firebaseUser
        .startActivityForReauthenticateWithProvider(/* activity= */ this, provider.build())
        .addOnSuccessListener(
            new OnSuccessListener<AuthResult>() {
                @Override
                public void onSuccess(AuthResult authResult) {
                // User is re-authenticated with fresh tokens and
                // should be able to perform sensitive operations
                // like account deletion and email or password
                // update.
                Map<String,Object> mapProfile = authResult.getAdditionalUserInfo().getProfile();
                Object displayName = mapProfile.get("displayName");

                statusTextView.setText("Welcome back, " + displayName);
                logger.info("Hello, " + displayName);
                signInButton.setVisibility(signInButton.INVISIBLE);
                microsoftSignInButton.setVisibility(microsoftSignInButton.INVISIBLE);
                microsoftSignOutButton.setVisibility(microsoftSignOutButton.VISIBLE);
                forwardButton.setVisibility(forwardButton.VISIBLE);
                openActivity2();
                }
            })
        .addOnFailureListener(
            new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                // Handle failure.
                statusTextView.setText("ERROR on Microsoft sign-in: " + e);
                }
            });
        }

完整步骤如下:https://firebase.google.com/docs/auth/android/microsoft-oauth

Platform Configuration

0 个答案:

没有答案
相关问题