Firebase:获取Facebook Photo Url [Android]

时间:2017-01-17 19:51:55

标签: android firebase firebase-authentication facebook-android-sdk

我正在使用Facebook身份验证与Firebase,我正在尝试从经过身份验证的用户获取Photo Url。我尝试过使用以下方法:

String photoUrl = firebaseUser.getPhotoUrl().toString();

这个方法:

String photoUrl = firebaseUser.getProviderData().get(0).getPhotoUrl().toString();

他们都返回格式为https://scontent.xx.fbcdn.net/v/t1.0-1/p100x100/1230538156_10205381561684678_351630538156623_n.jpg

的网址

当我尝试访问此网址时,收到以下消息:

“拒绝访问scontent.xx.fbcdn.net”

作为替代方案,我尝试获取用户的脸书ID,以便我可以使用图谱API请求照片,但似乎无法获得Facebook ID。我认为以下内容可行:

firebaseUser.getProviderData().get(0).getProviderId();

但这会返回字符串“firebase”??? ......嗯什么?

我的完整方法:

private void firebaseAuthWithFacebook(AccessToken token) {

        showProgressDialog("Signing in with Facebook","Signing in with Facebook");


        AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());

                        if(task.isSuccessful()){
                            FirebaseUser user = mAuth.getInstance().getCurrentUser();
                            if (user != null) {
                                addNewUserFacebook(user);
                            }

                        }
                        else{
                            Toast.makeText(SignInActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show();
                        }
                        hideProgressDialog();
                    }
                });
    }




private void addNewUserFacebook(FirebaseUser firebaseUser){
        String name = firebaseUser.getDisplayName();
        String email = firebaseUser.getEmail();

        /*My THREE failed methods - cue sad trombone */ 

        //String photoUrl = firebaseUser.getPhotoUrl().toString();
        //String photoUrl = firebaseUser.getProviderData().get(0).getPhotoUrl().toString();
        String photoUrl = "https://graph.facebook.com/" + firebaseUser.getProviderData().get(0).getProviderId() + "/picture?type=large";

        String providerId = "Facebook";
        final String uid = firebaseUser.getUid();

        User user = new User(name, email, photoUrl, providerId);

        rootRef.child("users").child(uid).setValue(user, new DatabaseReference.CompletionListener() {
            @Override
            public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                if (databaseError == null){
                    //add local user information
                    updateUser(uid);
                }

            }
        });

    }

1 个答案:

答案 0 :(得分:0)

好吧,我遇到了同样的问题,我在手机配置中删除了Facebook应用程序中的所有数据,没有数据,我试过,问题解决了,我觉得Facebook头晕目眩。

这种方法对我有用

String photoUrl =  firebaseUser.getProviderData().get(0).getPhotoUrl().toString();