GoogleSignInAccount getPhotoUrl() return 空

GoogleSignInAccount getPhotoUrl() return null

正在尝试从已登录的个人资料中获取照片。但始终 return 为空。姓名和电子邮件 return 值,只有照片有问题。

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

为什么会这样?帐户已签名,电子邮件和姓名已收到,但照片总是失败。

试试这个代码,我可以解决你的问题。

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);
    }
}

根据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.

请检查您的 Google 帐户是否有 Google+ 头像。

P/S:有时,如果 Google+ 个人资料图片已经创建,但在您在设备中添加 Google 帐户后,也许您需要从您的设备中删除现有 Google 帐户,然后重新添加。

if acct.getPhotoUrl() return null

表示您的 google 帐户未与 Google Plus 关联。如果您有适当的 google plus 帐户

,这不会给您 null

解决方案:

会有机会得到null ...下面给出解决方法

有一个休息网络服务,您可以通过它获取图像url

https://www.googleapis.com/plus/v1/people/{id}?fields=image&key={API_KEY}

"id" 您将通过 acct.getId() 方法调用获得 API_KEY 您将从您的开发者控制台获得...确保 API_KEY 是浏览器密钥....不是 android 或服务器密钥....

在开发者控制台中启用 Google+ Plus api...

现在您可以通过调用上述服务获取您的头像..

https://www.googleapis.com/plus/v1/people/{id}?fields=image&key={API_KEY}

您将收到如下回复

{ "image":{ "url": "https://lh4.googleusercontent.com/-StnGV_eLi3s/AAAAAAAAAAI/AAAAAAAABHU/XLH5wQ_Rm9E/photo.jpg?sz=50", "isDefault": 错误 } }

现在你得到了 url 作为 imageUrl....

现在请微笑并继续编码!!!

if(acct.getPhotoUrl() == null){
    //set default image
} else {
    photo_url = acct.getPhotoUrl().toString(); //photo_url is String
}

如果可能的话,你会在你的build.gradle中使用这个版本播放服务来编译,用这个改变你的旧实现:

implementation 'com.google.android.gms:play-services-auth:12.0.1'

之后您可以将此操作转化为 activity 结果:

@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);
  }
}

及以下:

private void handleSignInResult(GoogleSignInResult result) {
  GoogleSignInAccount acct = result.getSignInAccount();
  email       =   acct.getEmail();
  first_name  =   acct.getDisplayName();
  pic_profile = acct.getPhotoUrl().toString();
}

适合我!!

当我在我的 android 应用程序中发生这种情况时,我从设备上删除了帐户并将其放回;它奏效了。原来我的账号里没有照片。然后我加了一个。这是一个设备 运行 Jellybean。

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

因为@BNK 已经解释了其他原因

因此只需将我的解决方案添加到其中,以供面临同样问题的人使用(希望这对他们有所帮助)

同时使用以下代码(用 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

所以我只是像这样将 'requestIdToken' 添加到 GSO Builder 中:

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'

注意:请使用 'WebClientId' 作为 requestIdToken(),您可以从 (https://console.developers.google.com/apis/credentials?project=)