如何从谷歌loginAndroid中获取性别等信息?

我想集成谷歌login到我的应用程序,当用户第一次login时,我将创build一个帐户绑定到此,所以我需要一些configuration文件,如性别,区域设置等,我试图作为谷歌login文档和快速开始示例显示:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestEmail() .build(); mGoogleApiClient = new GoogleApiClient.Builder(this) .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */) .addApi(Auth.GOOGLE_SIGN_IN_API, gso) .build(); 

点击login后,我会打电话给:

  Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); startActivityForResult(signInIntent, RC_SIGN_IN); 

login成功后,我可以在onSignalResult中获取GoogleSignInResult的数据结构,从GoogleSignInResult获取GoogleSignInAccount,其中只包含DisplayName,email和id。 但是在https://developers.google.com/apis-explorer/#p/中 ,我可以获取性别,地区等个人资料。 有什么我错过了吗?

我试图谷歌加API,似乎我可以得到我想要的。 但不知道如何使用,文档说创build客户端是这样的:

 mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(Plus.API) .addScope(new Scope(Scopes.PLUS_LOGIN)) .addScope(new Scope(Scopes.PLUS_ME)) .build(); 

但是当我使用这个,点击loginbutton会导致应用程序崩溃。

更新:更新到Google新版本时出现问题loginGoogle服务3.0.0时缺lessapi_key /当前密钥

Solutions Collecting From Web of "如何从谷歌loginAndroid中获取性别等信息?"

更新:

由于Plus.PeopleApi已在Google Play服务9.4中作为Google的声明说明而被弃用,请参阅以下使用Google People API的解决scheme:

在新的谷歌login获取人的详细信息播放服务8.3 (伊莎贝拉陈的答案);

虽然明确的要求,但不能从Google Plus帐户获得私人生日

更新结束


首先,请确保您已经为自己的Google帐户创build了Google+个人资料。 那么你可以参考下面的代码:

 GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestScopes(new Scope(Scopes.PLUS_LOGIN)) .requestEmail() .build(); 

 mGoogleApiClient = new GoogleApiClient.Builder(this) .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */) .addApi(Auth.GOOGLE_SIGN_IN_API, gso) .addApi(Plus.API) .build(); 

然后

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); if (requestCode == RC_SIGN_IN) { GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); handleSignInResult(result); // G+ Person person = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient); Log.i(TAG, "--------------------------------"); Log.i(TAG, "Display Name: " + person.getDisplayName()); Log.i(TAG, "Gender: " + person.getGender()); Log.i(TAG, "AboutMe: " + person.getAboutMe()); Log.i(TAG, "Birthday: " + person.getBirthday()); Log.i(TAG, "Current Location: " + person.getCurrentLocation()); Log.i(TAG, "Language: " + person.getLanguage()); } } 

里面build.gradle文件

 // Dependency for Google Sign-In compile 'com.google.android.gms:play-services-auth:8.3.0' compile 'com.google.android.gms:play-services-plus:8.3.0' 

你可以看看我的GitHub示例项目 。 希望这可以帮助!

Plus人的东西已经被弃用了,不要再用了。 要做到这一点的方法是使用Google People API在您的项目中启用此API。 如果你不这样做,在Studio中抛出的exception包括一个直接链接到你的项目,以启用它(很好)。

在您的应用程序的build.gradle中包含以下依赖项:

 compile 'com.google.api-client:google-api-client:1.22.0' compile 'com.google.api-client:google-api-client-android:1.22.0' compile 'com.google.apis:google-api-services-people:v1-rev4-1.22.0' 

像正常一样做一个授权的GoogleSignIn。 除了基本账户以外,它不需要任何Scopes或Api

 GoogleSignInOptions.DEFAULT_SIGN_IN .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 

并要求电子邮件和configuration文件是你所需要的。

现在,一旦你有一个成功的login结果,你可以得到的帐户,包括电子邮件(也可以做这个ID也)。

 final GoogleSignInAccount acct = googleSignInResult.getSignInAccount(); 

现在是新部分:创build并执行一个AsyncTask,在获得acct电子邮件后调用Google People API 。

 // get Cover Photo Asynchronously new GetCoverPhotoAsyncTask().execute(Prefs.getPersonEmail()); 

这是AsyncTask:

 // Retrieve and save the url to the users Cover photo if they have one private class GetCoverPhotoAsyncTask extends AsyncTask<String, Void, Void> { HttpTransport httpTransport = new NetHttpTransport(); JacksonFactory jsonFactory = new JacksonFactory(); // Retrieved from the sigin result of an authorized GoogleSignIn String personEmail; @Override protected Void doInBackground(String... params) { personEmail = params[0]; Person userProfile = null; Collection<String> scopes = new ArrayList<>(Collections.singletonList(Scopes.PROFILE)); GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(SignInActivity.this, scopes); credential.setSelectedAccount(new Account(personEmail, "com.google")); People service = new People.Builder(httpTransport, jsonFactory, credential) .setApplicationName(getString(R.string.app_name)) // your app name .build(); // Get info. on user try { userProfile = service.people().get("people/me").execute(); } catch (IOException e) { Log.e(TAG, e.getMessage(), e); } // Get whatever you want if (userProfile != null) { List<CoverPhoto> covers = userProfile.getCoverPhotos(); if (covers != null && covers.size() > 0) { CoverPhoto cover = covers.get(0); if (cover != null) { // save url to cover photo here, load at will //Prefs.setPersonCoverPhoto(cover.getUrl()); } } } return null; } } 

这里是从人员可用的东西

如果将代码粘贴到项目中,请确保导入得到正确parsing。 有一些旧的API重叠的类名。

创build如下的谷歌login选项。 这对我来说是完美的

 GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestEmail() .requestProfile() .requestScopes(new Scope(Scopes.PLUS_ME)) .requestScopes(new Scope(Scopes.PLUS_LOGIN)) .build(); 

login后,执行:

  Plus.PeopleApi.load(mGoogleApiClient, acct.getId()).setResultCallback(new ResultCallback<People.LoadPeopleResult>() { @Override public void onResult(@NonNull People.LoadPeopleResult loadPeopleResult) { Person person = loadPeopleResult.getPersonBuffer().get(0); LogUtil.d("googleuser: getGivenName " + (person.getName().getGivenName())); LogUtil.d("googleuser: getFamilyName " + (person.getName().getFamilyName())); LogUtil.d("googleuser: getDisplayName " + (person.getDisplayName())); LogUtil.d("googleuser: getGender " + (person.getGender() + "")); LogUtil.d("googleuser: getBirthday " + (person.getBirthday() + "")); } });