Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GoogleSignInAccount getPhotoUrl() return null

Trying to get photo from signed in profile. But always return null. Name and email return values, trouble only with photo.

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestProfile()
            .requestEmail()
            .build();
mGoogleApiClient = new GoogleApiClient.Builder(StartActivity.this)
            .enableAutoManage(StartActivity.this, StartActivity.this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)   
            .build();
acct = gResult.getSignInAccount();
String name = acct.getDisplayName(); //okay, value != null
String email = acct.getEmail(); //okay, value != null
Uri photoUri = acct.getPhotoUrl() //not okay, value == null

Why does it happen so? Account signed, email and name got, but photo always fail.

like image 202
llaerto Avatar asked Nov 26 '15 06:11

llaerto


2 Answers

According to Google's documentation - GoogleSignInAccount

public Uri getPhotoUrl ()

Gets the photo url of the signed in user.

Returns

photo url for the Google account. Only non-null if requestProfile() is configured and user does have a Google+ profile picture.

Please check if your Google account has had Google+ profile picture or not.

P/S: sometimes, if Google+ profile picture has been created already but after the time you add Google account in your device, perhaps you need to delete that existing Google account from your device, then re-add.

like image 132
BNK Avatar answered Sep 18 '22 21:09

BNK


Was facing the same problem that is getting photoURL and tokenID as null

As @BNK had already explained what can be the other cause(s)

therefore just adding my solution into it, for someone who is facing the same problem (hoping, this help them)

While using below code (written in kotlin)

val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build()

I am just able to fetch information like 'DisplayName','Email','FamilyName','ID' except photoUrl and tokenId

So I just added 'requestIdToken' to GSO Builder like this:

val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestIdToken(resources.getString(R.string.googleAccountWebClientID)) // This line did the magic for me
            .build()

As a result of this I am able fetch all the information along with 'idToken' and 'photoUrl'

Note: Kindly use 'WebClientId' for requestIdToken() which you can get from (https://console.developers.google.com/apis/credentials?project=)

enter image description here

like image 41
A.R. Avatar answered Sep 19 '22 21:09

A.R.