代码之家  ›  专栏  ›  技术社区  ›  Oktawian

Firebase-使用Google帐户注册用户

  •  0
  • Oktawian  · 技术社区  · 7 年前

    我正在尝试将谷歌登录整合到我的应用程序中。我没有后端服务器,我只是在我的应用程序中获取登录谷歌账户的详细信息。

    我第一次用 Google Sign In Example 但我遇到了一个错误(除了打印下面的stacktrace之外,没有进行任何代码更改)。我只是使用了示例Signalivity,因为我没有后端服务器。

     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 签约报告

    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
    
    0 回复  |  直到 8 年前
        1
  •  4
  •   Владислав Шестернин    5 年前

    错误平台异常(登录失败,com.google.android.gms.common.api.ApiException:12500:,null)

    这个12500错误可以通过添加 支持电子邮件地址 到你的项目中 项目设置。 开放链接 https://console.firebase.google.com/

    选择项目并打开“设置”选项卡。

    请提供有效的支持电子邮件,然后立即重新启动应用程序。

    enter image description here

        2
  •  2
  •   Mohru    5 年前

    检查是否将SHA-1指纹添加到firebase项目设置中。如果没有,请使用查找SHA-1指纹

    https://developers.google.com/android/guides/client-auth

    此外,请使用查找释放密钥的SHA-1指纹

    keytool -list -v -keystore <keystore path>
    

    去除 <keystore path> 与密钥存储的路径有关。

    然后将两个SHA-1指纹添加到firebase项目设置中。

    注意:别忘了更换谷歌服务。json与更新的谷歌服务。带有新指纹的json。我为此损失了两天时间。

    调试时

    Android studio自动 generate ~/.android/debug.keystore 首先 调试构建 并用它来签署应用程序。

    获取SHA-1运行(密码 android ) ( doc ):

    keytool -exportcert -list -v -alias androiddebugkey -keystore ~/.android/debug.keystore
    

    SHA-1 应添加到位于的应用程序设置中 firebase 允许在测试调试构建时使用google登录功能。

        3
  •  1
  •   Brian Mahecha    4 年前

     对于错误12500,您需要仅在firebase设置中添加gmail支持,对于错误10,请在firebase控制台中添加ssh指纹,如图所示