我正在使用 google people API 登录和检索 google 帐户的个人数据。这是我获取人员数据的代码:
protected Void getData(GoogleSignInAccount acct) {
profile.setEmail(acct.getEmail());
profile.setName(acct.getDisplayName());
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(
context, Collections.singleton(Scopes.PROFILE)
);
credential.setSelectedAccount(new Account(acct.getEmail(), "com.google"));
People service = new People.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(context.getString(R.string.application_name))
.build();
Person meProfile = null;
try {
//here is the problem in release
meProfile = service.people().get("people/me").execute();
} catch (IOException e) {
Logger.d(TAG, e);// it returns 404 Not Found
Logger.writeLog(LoginActivityNew.this, TAG, "e msg - " + e.getMessage());
}
if (meProfile != null) {
if (UtilCommon.isEmpty(profile.getName())) {
List<Name> names = meProfile.getNames();
if (names != null) {
for (Name name : names) {
if (!UtilCommon.isEmpty(name.getDisplayName())) {
profile.setName(name.getDisplayName());
break;
}
}
}
}
List<Birthday> birthdays = meProfile.getBirthdays();
if (birthdays != null && birthdays.size() > 0) {
for (Birthday bDay : birthdays) {
if (bDay == null || bDay.getDate() == null) continue;
Date date = bDay.getDate();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, date.getDay());
calendar.set(Calendar.MONTH, date.getMonth());
calendar.set(Calendar.YEAR, date.getYear());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
profile.setDOB(calendar.getTime());
break;
}
}
List<Gender> genders = meProfile.getGenders();
if (genders != null && genders.size() > 0) {
profile.setSex(Profile.SEX_NONE);
for (Gender genderObj : genders) {
if (genderObj == null || UtilCommon.isEmpty(genderObj.getValue())) continue;
String gender = genderObj.getValue();
if (gender.equals("male"))
profile.setSex(Profile.SEX_MALE);
else if (gender.equals("female"))
profile.setSex(Profile.SEX_FEMALE);
else if (gender.equals("unknown"))
profile.setSex(Profile.SEX_NONE);
else
profile.setSex(Profile.SEX_OTHERS);
break;
}
}
Logger.writeLog(LoginActivityNew.this, TAG, "name - " + profile.getName());
Logger.writeLog(LoginActivityNew.this, TAG, "gender - " + profile.getSex());
if (profile.getDOB() != null)
Logger.writeLog(LoginActivityNew.this, TAG, "dob - " + profile.getDOB());
}
}
尽管谷歌登录过程运行良好,但这里meProfile = service.people().get("people/me").execute();
发生了一件有趣的事情。
它在调试模式下完美运行,但在发布模式下却不行。
使用释放模式时,此块返回异常:404 Not Found
在此服务器上找不到请求的 URL。
我检查了谷歌控制台项目并为调试和发布密钥提供了有效的 SHA-1 证书。
有人知道这个问题吗?我被困住了。