将Google帐户与firebase

时间:2016-11-19 23:02:40

标签: android firebase firebase-authentication

我想在将Google帐户与当前帐户(匿名)关联但无法获取后,获取显示名称和照片网址。如果我使用Google帐户直接登录而不将其链接到匿名帐户,则可以完美地加载显示名称和照片 这里是我的链接帐户代码: -

    private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());

     credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    auth.getCurrentUser().linkWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()){

                        Intent intent = new Intent(activity, SetupActivity.class);
                        startActivity(intent);
                        finish();
                    }
                    Log.d(TAG, "signInWithCredential:onComplete:---------" + task.isSuccessful());

                    progressBar.setVisibility(View.GONE);

                    if (!task.isSuccessful()) {

                        Log.w(TAG, "signInWithCredential-------", task.getException());
                        Toast.makeText(activity, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            });


}

此处可直接登录Google: -

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());

    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    auth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());

                    progressBar.setVisibility(View.GONE);


                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                    if (!task.isSuccessful()) {
                        Log.w(TAG, "signInWithCredential", task.getException());
                        Toast.makeText(LoginActivity.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            });


}

此代码用于获取照片和名称

if (firebaseUser != null){
        if (firebaseUser.getPhotoUrl() != null){
            Log.d(TAG, "photo url "+ firebaseUser.getPhotoUrl());
            final String dpImage = firebaseUser.getPhotoUrl().toString();
            if (dpImage.length() > 0) {
                Picasso.with(SetupActivity.this).load(dpImage)
                        .networkPolicy(NetworkPolicy.OFFLINE)
                        .into(profilePic_setup, new Callback() {
                            @Override
                            public void onSuccess() {

                            }

                            @Override
                            public void onError() {
                                //Try again online if cache failed
                                Picasso.with(SetupActivity.this)
                                        .load(dpImage)
                                        .error(R.drawable.ic_account_circle_black_50dp)
                                        .into(profilePic_setup, new Callback() {
                                            @Override
                                            public void onSuccess() {

                                            }

                                            @Override
                                            public void onError() {
                                                Log.v("Picasso","Could not fetch image");
                                            }
                                        });
                            }
                        });

            }
        }
        if (firebaseUser.getDisplayName() != null){

            userNameSetup.setText(firebaseUser.getDisplayName());
        }
    }

我还想知道Google凭据是否已经链接到另一个用户帐户,如何将当前帐户合并到该帐户 我尝试使用它但不工作

FirebaseUser prevUser = currentUser;
currentUser = auth.signInWithCredential(credential).await().getUser();
// Merge prevUser and currentUser accounts and data
// ... 

它显示await()方法无法重新载入

0 个答案:

没有答案