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.
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.
if acct.getPhotoUrl() return null
means your google account is not associated with Google Plus.. This will not give you null if you have a proper google plus account
Solution:
There will be a chance to get null ...Solution is given below
there is one rest webservice by which you will get your imageurl
https://www.googleapis.com/plus/v1/people/{id}?fields=image&key={API_KEY}
"id" you will get by acct.getId() method calling and API_KEY you will get from your developer console...make sure that API_KEY is Browser key....not android or server key....
Enable Google+ Plus api there itself in developer console...
Now you can get your profile image via call above service..
https://www.googleapis.com/plus/v1/people/{id}?fields=image&key={API_KEY}
you will get a response like below
{ "image": { "url": "https://lh4.googleusercontent.com/-StnGV_eLi3s/AAAAAAAAAAI/AAAAAAAABHU/XLH5wQ_Rm9E/photo.jpg?sz=50", "isDefault": false } }
Now you got your url as imageUrl....
Now Smile Please and keep coding!!!
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=)
Try this code I this its work for your problem.
String _name;
@Override
public void onConnected(Bundle arg0) {
Log.e(TAG, "onConnected");
Plus.PeopleApi.loadVisible(mGoogleApiClient, null).setResultCallback(this);
Person currentUser = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
Log.e("USERNAME_Con",Plus.AccountApi.getAccountName(mGoogleApiClient));
Log.e("USERNAME_Con",currentUser.getBirthday()+" "+currentUser.getImage().getUrl()+" "+currentUser.getId());
String personPhotoUrl=currentUser.getImage().getUrl();
if(currentUser!=null){
String email=Plus.AccountApi.getAccountName(mGoogleApiClient);
String id=currentUser.getId();
_name=currentUser.getDisplayName();
progressDialog = ProgressDialog.show(Form.this, "", "Loading...", true,false);
logingoogle(email,id,_name);
}
personPhotoUrl = personPhotoUrl.substring(0,
personPhotoUrl.length() - 2)
+ 400;
Log.e("USER Image",""+personPhotoUrl);
new LoadProfileImage().execute(personPhotoUrl);
// Indicate that the sign in process is complete.
mSignInProgress = STATE_DEFAULT;
}
private class LoadProfileImage extends AsyncTask<String, Void, Bitmap> {
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
camera=mIcon11;
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
// bmImage.setImageBitmap(result);
setimage(camera);
}
}
if(acct.getPhotoUrl() == null){
//set default image
} else {
photo_url = acct.getPhotoUrl().toString(); //photo_url is String
}
If possible only you would compile with this version play services in your build.gradle change your old implementation with this:
implementation 'com.google.android.gms:play-services-auth:12.0.1'
after that you can do this action into activity result:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result =Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
and below:
private void handleSignInResult(GoogleSignInResult result) {
GoogleSignInAccount acct = result.getSignInAccount();
email = acct.getEmail();
first_name = acct.getDisplayName();
pic_profile = acct.getPhotoUrl().toString();
}
Works for me!!
When this happened to me in my android app I deleted the account off the device and put it back; and it worked. Originally I had no photo in the account. Then I added one. It was a device running Jellybean.
来源:https://stackoverflow.com/questions/33931578/googlesigninaccount-getphotourl-return-null
