一、注册应用

SHA-1 必须添加

image.png

二、下载配置文件

image.png

三、添加SDK


1. 项目级 build.gradle

  1. buildscript {
  2. dependencies {
  3. // Add this line
  4. classpath 'com.google.gms:google-services:4.2.0'
  5. }
  6. }

2. 应用级 build.gradle

  1. dependencies {
  2. // Add this line
  3. implementation 'com.google.firebase:firebase-core:16.0.9'
  4. implementation 'com.google.firebase:firebase-auth:16.1.0'
  5. implementation 'com.google.android.gms:play-services-auth:16.0.1'
  6. }
  7. ...
  8. // Add to the bottom of the file
  9. apply plugin: 'com.google.gms.google-services'

四、启用Google登录功能

image.png

五、使用 Firebase 进行Google身份验证

  1. private static final String TAG = "MainActivity";
  2. private int RC_SIGN_IN = 1;
  3. private GoogleSignInClient mGoogleSignInClient;
  4. private FirebaseAuth mAuth;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
  10. // 服务器的客户端 ID
  11. .requestIdToken(getString(R.string.default_web_client_id))
  12. .requestEmail()
  13. .build();
  14. mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
  15. mAuth = FirebaseAuth.getInstance();
  16. // 谷歌登录按钮
  17. findViewById(R.id.btn_login).setOnClickListener(new View.OnClickListener() {
  18. @Override
  19. public void onClick(View v) {
  20. signIn();
  21. }
  22. });
  23. }
  24. /**
  25. * 调用谷歌登陆
  26. */
  27. private void signIn() {
  28. Intent signInIntent = mGoogleSignInClient.getSignInIntent();
  29. startActivityForResult(signInIntent, RC_SIGN_IN);
  30. }
  31. @Override
  32. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  33. super.onActivityResult(requestCode, resultCode, data);
  34. //
  35. if (requestCode == RC_SIGN_IN) {
  36. Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
  37. try {
  38. // 谷歌登陆成功,获取谷歌账号信息
  39. GoogleSignInAccount account = task.getResult(ApiException.class);
  40. firebaseAuthWithGoogle(account);
  41. } catch (ApiException e) {
  42. // 谷歌登录失败
  43. Log.e(TAG, "Google sign in failed", e);
  44. }
  45. }
  46. }
  47. private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
  48. Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
  49. // showProgressDialog();
  50. // 异步任务
  51. AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
  52. mAuth.signInWithCredential(credential)
  53. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  54. @Override
  55. public void onComplete(@NonNull Task<AuthResult> task) {
  56. if (task.isSuccessful()) {
  57. // 登录成功
  58. Log.d(TAG, "signInWithCredential:success");
  59. final FirebaseUser user = mAuth.getCurrentUser();
  60. Log.d(TAG, "uid:" + user.getUid());
  61. Log.d(TAG, "email:" + user.getEmail());
  62. Log.d(TAG, "name:" + user.getDisplayName());
  63. Log.d(TAG, "phone:" + user.getPhoneNumber());
  64. Log.d(TAG, "providerid:" + user.getProviderId());
  65. Toast.makeText(MainActivity.this, user.getUid(), Toast.LENGTH_SHORT).show();
  66. } else {
  67. // 登录失败
  68. Log.e(TAG, "signInWithCredential:failure", task.getException());
  69. Toast.makeText(MainActivity.this, "Authentication Failed.", Toast.LENGTH_SHORT).show();
  70. }
  71. // hideProgressDialog();
  72. }
  73. });
  74. }

六、常见问题

1. 12500异常

添加支持电子邮件地址地址

image.png