Google 人们 API 在发布模式下不起作用 android
Google People API doesn't work in release mode android
我正在使用 google 人 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());
}
}
虽然 Google 登录过程正常,但在 meProfile = service.people().get("people/me").execute();
块中发生了一件有趣的事情。
It works perfectly while in debug mode but not in release mode.
使用释放模式时,此块 returns 异常:404 Not Found
The requested URL was not found on this server.
我检查了 google 控制台项目并为调试和发布密钥提供了有效的 SHA-1 证书。
有人知道这个问题吗?我卡住了。
您是说该应用无法与 signed/exported apk 一起使用?
如果是这种情况,
您需要将签名的 SHA 密钥添加到您的 Google 应用帐户。
https://developers.google.com/people/v1/how-tos/authorizing
您可以像这样找到您的签名 SHA 密钥 -
keytool -exportcert -keystore path-to-debug-or-production-keystore -list -v
通过禁用 proguard 生成签名的 APK(如果您启用了它)。您可以通过在 build.gradle 文件中设置 minifyEnabled = false 来实现。
如果问题得到解决,那么它是一个与proguard相关的问题。
您可以使用 keep class 属性解决此问题,包括 API 相关 classes 在 proguard 文件中。
这是一个混淆器相关的问题。
解决这个问题只需做 = minifyEnabled false
转到 build.gradle(应用程序)
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
我正在使用 google 人 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());
}
}
虽然 Google 登录过程正常,但在 meProfile = service.people().get("people/me").execute();
块中发生了一件有趣的事情。
It works perfectly while in debug mode but not in release mode.
使用释放模式时,此块 returns 异常:404 Not Found
The requested URL was not found on this server.
我检查了 google 控制台项目并为调试和发布密钥提供了有效的 SHA-1 证书。
有人知道这个问题吗?我卡住了。
您是说该应用无法与 signed/exported apk 一起使用? 如果是这种情况, 您需要将签名的 SHA 密钥添加到您的 Google 应用帐户。
https://developers.google.com/people/v1/how-tos/authorizing
您可以像这样找到您的签名 SHA 密钥 -
keytool -exportcert -keystore path-to-debug-or-production-keystore -list -v
通过禁用 proguard 生成签名的 APK(如果您启用了它)。您可以通过在 build.gradle 文件中设置 minifyEnabled = false 来实现。
如果问题得到解决,那么它是一个与proguard相关的问题。
您可以使用 keep class 属性解决此问题,包括 API 相关 classes 在 proguard 文件中。
这是一个混淆器相关的问题。
解决这个问题只需做 = minifyEnabled false
转到 build.gradle(应用程序)
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}