我正在尝试将Google登录集成到我的应用中 . 我没有后端服务器,我只是将登录的Google帐户的详细信息提供给我的应用程序 .
我首先尝试使用Google Sign In Example但是我收到了一个错误(除了打印下面的堆栈跟踪之外没有进行任何代码更改) . 我只使用了示例SignInActivity,因为我没有后端服务器 .
Exception com.google.android.gms.common.api.ApiException: 12500:
at com.google.android.gms.common.internal.zzb.zzz(Unknown Source)
at com.google.android.gms.auth.api.signin.GoogleSignIn.getSignedInAccountFromIntent(Unknown Source)
at com.ewise.android.api.MainActivity.onActivityResult(SignInActivity.java:89) at android.app.Activity.dispatchActivityResult(Activity.java:7010)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4187)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4234)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1584)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6316)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
码
public class SignInActivity extends AppCompatActivity implements
View.OnClickListener {
private static final String TAG = "SignInActivity";
private static final int RC_SIGN_IN = 9001;
private GoogleSignInClient mGoogleSignInClient;
private TextView mStatusTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Views
mStatusTextView = findViewById(R.id.status);
// Button listeners
findViewById(R.id.sign_in_button).setOnClickListener(this);
findViewById(R.id.sign_out_button).setOnClickListener(this);
findViewById(R.id.disconnect_button).setOnClickListener(this);
// [START configure_signin]
// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
// [END configure_signin]
// [START build_client]
// Build a GoogleSignInClient with the options specified by gso.
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
// [END build_client]
// [START customize_button]
// Set the dimensions of the sign-in button.
SignInButton signInButton = findViewById(R.id.sign_in_button);
signInButton.setSize(SignInButton.SIZE_STANDARD);
signInButton.setColorScheme(SignInButton.COLOR_LIGHT);
// [END customize_button]
}
@Override
public void onStart() {
super.onStart();
// [START on_start_sign_in]
// Check for existing Google Sign In account, if the user is already signed in
// the GoogleSignInAccount will be non-null.
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
updateUI(account);
// [END on_start_sign_in]
}
// [START onActivityResult]
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
// The Task returned from this call is always completed, no need to attach
// a listener.
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
// [END onActivityResult]
// [START handleSignInResult]
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
// Signed in successfully, show authenticated UI.
updateUI(account);
} catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
e.printStackTrace();
updateUI(null);
}
}
// [END handleSignInResult]
// [START signIn]
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
// [END signIn]
// [START signOut]
private void signOut() {
mGoogleSignInClient.signOut()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// [START_EXCLUDE]
updateUI(null);
// [END_EXCLUDE]
}
});
}
// [END signOut]
// [START revokeAccess]
private void revokeAccess() {
mGoogleSignInClient.revokeAccess()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// [START_EXCLUDE]
updateUI(null);
// [END_EXCLUDE]
}
});
}
// [END revokeAccess]
private void updateUI(@Nullable GoogleSignInAccount account) {
if (account != null) {
mStatusTextView.setText(getString(R.string.signed_in_fmt, account.getDisplayName()));
findViewById(R.id.sign_in_button).setVisibility(View.GONE);
findViewById(R.id.sign_out_and_disconnect).setVisibility(View.VISIBLE);
} else {
mStatusTextView.setText(R.string.signed_out);
findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
findViewById(R.id.sign_out_and_disconnect).setVisibility(View.GONE);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sign_in_button:
signIn();
break;
case R.id.sign_out_button:
signOut();
break;
case R.id.disconnect_button:
revokeAccess();
break;
}
}
}
从我读到的,问题可能是由SHA1 Generation引起的 .
我跟着完整的guide,但显然它不起作用 .
我从gradle中复制了SHA1 signingReport
Variant: debug
Config: debug
Store: /Users/user/.android/debug.keystore
Alias: AndroidDebugKey
MD5: A3:16:3F:43:75:FE:07:62:6D:8D:CC:DD:21:9F:FA:1A
SHA1: 7B:21:26:7F:D8:18:BB:0E:36:1C:82:DD:B7:28:5F:C1:2F:5C:E4:EA
Valid until: Saturday, August 31, 2047
----------
Variant: release
Config: none
----------
Variant: debugAndroidTest
Config: debug
Store: /Users/user/.android/debug.keystore
Alias: AndroidDebugKey
MD5: A3:16:3F:43:75:FE:07:62:6D:8D:CC:DD:21:9F:FA:1A
SHA1: 7B:21:26:7F:D8:18:BB:0E:36:1C:82:DD:B7:28:5F:C1:2F:5C:E4:EA
Valid until: Saturday, August 31, 2047
----------
Variant: debugUnitTest
Config: debug
Store: /Users/user/.android/debug.keystore
Alias: AndroidDebugKey
MD5: A3:16:3F:43:75:FE:07:62:6D:8D:CC:DD:21:9F:FA:1A
SHA1: 7B:21:26:7F:D8:18:BB:0E:36:1C:82:DD:B7:28:5F:C1:2F:5C:E4:EA
Valid until: Saturday, August 31, 2047
可能的原因是什么?
谢谢
附:这可能是一个可能的原因吗?
Google Play services out of date. Requires 11720000 but found 10932470
11 回答
只需将您的Google Play服务更新为最新版本(在本例中为11720000) . 如果您使用AVD,Nexus 5和5X图像支持Google Play . 模拟器启动并运行后,转到扩展控件菜单> Google Play,然后更新 .
检查是否将SHA-1指纹添加到firebase项目设置中 . 如果没有,请使用找到SHA-1指纹
https://developers.google.com/android/guides/client-auth
另外,使用找到释放密钥的SHA-1指纹
使用密钥库的路径删除
<keystore path>
.然后将两个SHA-1指纹添加到firebase项目设置中 .
注意:不要忘记用更新的指纹替换google-services.json和更新的google-services.json . 我失去了两天 .
似乎您的SHA1被Google Play商店覆盖 . 检查您的Google Play商店,启动面板,在应用签名下,查看Google Play是否添加了额外的SHA1 .
并复制SHA1,添加到您的相关位置,将完成这项工作 .
如果仍有人遇到类似问题,如果您要添加自定义范围,请确保它是有效范围 . 就我而言,我将Facebook范围与谷歌范围混合在一起并花了一些时间来弄清楚它!
我认为错误来自错误的SHA1 . 请不要忘记在android studio中发布和调试模式之间的SHA1是不同的 . 而不是使用keytool来获取SHA1,你可以在android studio中使用Gradle项目 - >任务 - > android - > signingReport(可以通过菜单View - > Toolwindow - > gradle打开它)来获得释放和调试SHA1 . 之后,为了便于工作,您需要在Google Cloud 端控制台上创建两个单独的凭据和两个SHA1(谷歌只是指示使用版本SHA1创建1,当我们开发它时,它将无法工作,因为它使用调试SHA1) .
对我来说,问题是在我的调试配置应用中使用'release'ClientID . 确保您有一个版本和一个调试密钥,分别使用每个SHA-1 .
It can also happen that the cordova compiler is unable to find the proper keystore file.
Solution: 在执行
ionic cordova build android
之前指定 signing propertiesStep-1 :生成调试密钥库文件
执行命令
使用密码:
android
Step-2: 将密钥库文件(
debug.keystore
)从~/.android
复制到当前项目的platform/android
目录Step-3: 在
platform/android
目录中创建名为 release-signing.properties 的文件Step-4: 添加文件中的内容
Step-5: 现在 Build
ionic cordova build android
在我的情况下,这是因为错误的Google客户端ID . 我将我的密钥更改为
google-services.json
中列出的密钥(在oauth_client
对象下)确保正确设置以下内容:
在Google项目中生成客户端ID .
为该客户端ID提供适当的SHA-1密钥 . (调试/发布)
为该客户端ID提供正确的包名称 .
确保您已在
strings.xml
,google-services.json
或credentials.json
文件中生成了客户端ID .在另一台计算机(不同的Android Studio)上打开我的项目后,我遇到了同样的问题 . 在我的情况下,我使用Firebase助手解决了它,我最初用它来设置Firebase . 打开Firebase智能助理(工具> Firebase),然后选择身份验证>连接 . 这将项目重新连接到Firebase并更新了配置
试试这个选项:
它会提示输入密码并输入密码 . 您可以看到SHA1,MD5指纹 .