0

I am building a Kotlin app and am using FirebaseAuth for login. I activated Facebook login via Firebase and it works fine. My problem is that the created Firebase user's photoUrl: (https://graph.facebook.com/<UserId>/picture) is pointing to a tiny version of it's Facebook profile picture, instead of a normal sized one. I found this solution: https://stackoverflow.com/a/52099896/13674106. The Google part of this answer worked perfectly for me, but in the Facebook case I am now getting a default avatar image (in the right resolution). My code looks like this:

    fun handleSignInSucceeded(dataIntent: Intent?) {
        val response = IdpResponse.fromResultIntent(dataIntent)

        // For new created users, check if a photo is available from the auth provider
        if (response?.isNewUser == true) {
            // All users have a default FirebaseAuth provider data - we want to check which is the
            // other one
            currentUser.value?.providerData
                ?.find { it.providerId != FirebaseAuthProvider.PROVIDER_ID }?.apply {
                    val photoUrl = when (providerId) {
                        GoogleAuthProvider.PROVIDER_ID ->
                            photoUrl.toString().replace("s96-c", "s400-c").toUri()
                        FacebookAuthProvider.PROVIDER_ID ->
                            photoUrl.toString().plus("?type=large").toUri()
                        else -> null
                    }
                    photoUrl?.let { updatePhotoUri(it, false) }
                }
        }
    }

fun updatePhotoUri(photoUrl: Uri, uploadToStorage: Boolean = true): Task<Void>? {
        val profileUpdates = UserProfileChangeRequest.Builder()

        return if (uploadToStorage)    ...
        else updateProfile(profileUpdates.setPhotoUri(photoUrl).build())
    }

private fun updateProfile(profileUpdates: UserProfileChangeRequest): Task<Void>? {
        return currentUser.value?.updateProfile(profileUpdates)
            ?.addOnCompleteListener {
                if (it.isSuccessful) {
                    Log.d(TAG, "User profile updated.")
                    _currentUser.value = firebaseAuth.currentUser
                }
            }
    }

Where _currentUser is a private MutableLiveData<FirebaseUser?> shadowed by a public LiveData<FirebaseUser?> called user which is binded to my ImageView on the fragment:

<ImageView
                    android:id="@+id/image_profile_picture"
                    ...
                    app:userPhotoSrc="@{profileViewModel.user}" />

Where userPhotoSrc is implemented by the following BindingAdapter using Glide:

@BindingAdapter("userPhotoSrc")
fun ImageView.setUserPhotoSrc(user: FirebaseUser?) {
    Glide.with(context)
        .load(user?.photoUrl)
        .circleCrop()
        .fallback(R.drawable.ic_profile_black_24)
        .into(this)
}

I checked in debug and the value of user?.photoUrl at that point is https://graph.facebook.com/<UserId>/picture?type=large as expected. I found this thread: Retrieving Default Image All Url Profile Picture from Facebook Graph API, Which shows a problem similar to mine, but according to the answers there, my code should work by now. I also tried to use the height parameter instead of type, like stated in this answer: https://stackoverflow.com/a/50710161/13674106, But I got the same result.

Please help me solve it, Omer

Omer Levy
  • 347
  • 4
  • 11
  • 1
    Try if appending the access token to the profile picture URL helps. Facebook is in the process of making that a requirement, it is supposed to go live for all app on 2020-10-24, but with a newly created app id(?) it might apply before already. https://developers.facebook.com/docs/graph-api/reference/user/picture – 04FS Oct 05 '20 at 07:11
  • That worked, thanks! I didn't try that because getting the tiny image kept on working so I it's weird that it is an authentication problem. Do you have an idea why it worked? Just so I can understand it better. – Omer Levy Oct 06 '20 at 11:34

0 Answers0