Gmail登录到Android应用程序的集成,profilePhoto始终为null?

时间:2017-04-25 18:15:13

标签: android google-plus gmail-api

我们已将Gmail登录集成到我们的Android应用程序中,我们正在使用服务器端集成来获取一次auth_code,如下所述:https://developers.google.com/identity/sign-in/android/offline-access

我们在服务器端以及应用程序内的Android设备上都可以访问Gmail帐户。

但不知怎的,我们没有使用以下方法获取个人资料照片:

googleSignInAccount.getPhotoUrl();

在提出请求时,我们使用以下代码:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                        .requestScopes(new Scope(GmailScopes.GMAIL_READONLY))
                        .requestServerAuthCode("OUR_SERVER_CLIET_ID", true)
                        .requestEmail()
                        .requestProfile()
                        .build();

有关stackoverflow的一些问题表明,如果用户启用了Google+帐户并配置了个人资料照片,那么只有您才能获得照片网址。这种情况在我们的案例中没有发生,因为我们使用在Google+帐户上有照片的帐户进行了测试。

我们在iOS应用程序中进行了类似的集成,我们可以在其中获取个人资料照片网址?什么可能是错的?

更新:

只有在个人资料中设置了照片集的用户才能获得照片网址,否则为空。

3 个答案:

答案 0 :(得分:0)

通过使用以下代码段,我获得了用户的帐户ID,电子邮件,显示名称,photourl

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build();
            mGoogleApiClient = new GoogleApiClient.Builder(mContext).enableAutoManage((AppCompatActivity) activity, new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

                }
            }).addApi(Auth.GOOGLE_SIGN_IN_API, gso).build();
            Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
            startActivityForResult(signInIntent,1);

在onActivityResult中,我在acct中获取这些值

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            if (result.isSuccess()) {
                GoogleSignInAccount acct = result.getSignInAccount();
                mGoogleApiClient.stopAutoManage((AppCompatActivity) activity);
                mGoogleApiClient.disconnect();
                Log.e("user_id",acct.getId());
                Log.e("user_id",acct.getEmail());
                Log.e("user_id",acct.getDisplayName());
                Log.e("image_url",String.valueOf(acct.getPhotoUrl()));
        }
    }

相应地修改了googlesignin构建器。

答案 1 :(得分:0)

更新:

只有在个人资料中设置了照片集的用户才能获得照片网址,否则为空。

答案 2 :(得分:0)

如果任何用户没有设置任何个人资料图片,那么它将始终为空。所以你需要在你的应用程序中处理这种情况。

if(acct.getPhotoUrl()!=null)
{
   personPhotoUrl = acct.getPhotoUrl().toString();
}