Android应用中如何通过Google Sign-In授权后调用PeopleAPI获取用户生日、性别等信息?
Android应用中如何通过Google Sign-In授权后调用PeopleAPI获取用户生日、性别等信息?
嘿,我之前也碰到过一模一样的问题!Google Sign-In本身只能返回邮箱、名字这类基础信息,要拿生日、性别这类敏感数据确实得靠People API,而且授权衔接其实没那么复杂,我给你一步步捋清楚:
第一步:先确认你已经请求了正确的权限范围(Scopes)
首先得确保配置Google Sign-In时,已经把获取生日、性别的Scopes加进去了——不然用户就算同意登录,也没给你权限拿这些数据。比如:
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestEmail() // 添加生日和性别读取权限 .requestScopes(new Scope("https://www.googleapis.com/auth/user.birthday.read")) .requestScopes(new Scope("https://www.googleapis.com/auth/user.gender.read")) .build();
第二步:添加People API的依赖
在你的app模块build.gradle里加上People API的依赖(用最新版本就行):
implementation 'com.google.apis:google-api-services-people:v1-rev20240208-2.0.0'
第三步:用GoogleSignInAccount的授权信息初始化People API客户端
登录成功拿到GoogleSignInAccount之后,你可以从这个对象里拿到用户的Account实例,用它来构建People API的授权凭证——这样就不用让用户重新授权了,因为Google Sign-In已经帮你完成了授权流程:
// 登录成功回调里拿到的GoogleSignInAccount对象 GoogleSignInAccount signInAccount = GoogleSignIn.getLastSignedInAccount(this); if (signInAccount != null) { // 获取用户的Google账号实例 Account googleAccount = signInAccount.getAccount(); // 构建授权凭证,注意这里的Scopes要和登录请求时的完全一致 GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2( getApplicationContext(), Arrays.asList( Scopes.PROFILE, "https://www.googleapis.com/auth/user.birthday.read", "https://www.googleapis.com/auth/user.gender.read" ) ); credential.setSelectedAccount(googleAccount); // 初始化PeopleService客户端 People peopleService = new People.Builder(AndroidHttp.newCompatibleTransport(), GsonFactory.getDefaultInstance(), credential) .setApplicationName("你的应用名称") // 换成你的App名字 .build(); }
第四步:调用People API获取用户数据
拿到PeopleService之后,就可以异步请求用户的生日、性别信息了(注意:网络请求不能在主线程做,用AsyncTask或者协程都行):
new AsyncTask<Void, Void, Person>() { @Override protected Person doInBackground(Void... voids) { try { // 请求用户个人资料,指定要获取的字段:生日、性别、名字、邮箱 return peopleService.people().get("people/me") .setPersonFields("birthdays,genders,names,emailAddresses") .execute(); } catch (IOException e) { e.printStackTrace(); return null; } } @Override protected void onPostExecute(Person person) { if (person != null) { // 处理生日数据 List<Birthday> birthdays = person.getBirthdays(); if (birthdays != null && !birthdays.isEmpty()) { Birthday birthday = birthdays.get(0); Date birthDate = birthday.getDate(); // 这里可以拿到年、月、日:birthDate.getYear(), birthDate.getMonth(), birthDate.getDay() Log.d("UserInfo", "生日:" + birthDate.getYear() + "-" + birthDate.getMonth() + "-" + birthDate.getDay()); } // 处理性别数据 List<Gender> genders = person.getGenders(); if (genders != null && !genders.isEmpty()) { Gender gender = genders.get(0); String genderValue = gender.getValue(); // 取值可能是"male"、"female"、"other"等 Log.d("UserInfo", "性别:" + genderValue); } } } }.execute();
几个关键注意点
- 别忘了在Google Cloud Console里启用People API!不然调用会返回权限错误。
- 确保请求的Scopes完全一致:登录时请求的Scopes和初始化
GoogleAccountCredential时的Scopes必须一模一样,不然会报权限不足。 - 用户必须在登录弹窗里同意了这些权限,不然People API返回的
birthdays或genders会是空列表。
备注:内容来源于stack exchange,提问作者Daje
相关产品推荐
相关产品推荐

