Android Firebase身份驗證 – Google登錄
Firebase身份驗證 提供了后端服務,通過使用SDK和內置UI庫來輕松驗證應用程序中的用戶。愛掏網 - it200.com大多數應用程序都需要用戶的身份,了解其狀態后,應用程序會將用戶數據安全地保存在云中。愛掏網 - it200.com它旨在構建一個安全的身份驗證系統。愛掏網 - it200.com
使用Firebase身份驗證,我們將對Google、Facebook、GitHub、Twitter等進行身份驗證登陸。愛掏網 - it200.com
在本教程中,我們將在我們的Android應用程序中集成Firebase身份驗證以實現Google登錄功能,使用 Google 和 Firebase APIs 。愛掏網 - it200.com
1. 在 https://firebase.google.com/ 上創建Firebase開發人員帳戶,并點擊“轉到控制臺”。愛掏網 - it200.com
2. 點擊 ‘添加項目’ 。愛掏網 - it200.com
3. 填寫項目名稱,選擇分析位置、云Firestore位置,接受控制器條款并點擊 ‘創建項目’ 。愛掏網 - it200.com
4. 當您的新項目成功準備好時,請點擊 ‘繼續’ .
5. 選擇 ‘Android’ 平臺的SDK。愛掏網 - it200.com
6. 通過提供所需的應用程序信息并點擊 ‘注冊應用’ 來將您的應用程序注冊到Firebase。愛掏網 - it200.com
我們可以通過以下步驟獲取應用程序證書SHA-1密鑰:
- 打開Android工程。愛掏網 - it200.com
- 從右側面板中打開Gradle選項卡。愛掏網 - it200.com
- 雙擊 ‘signingReport’ 。愛掏網 - it200.com
- 我們將在 ‘Gradle控制臺’ 中找到我們的應用程序SHA-1密鑰。愛掏網 - it200.com
7. 現在下載 ‘google-services.json’ 文件并將其集成到Android應用程序中,然后點擊 ‘下一步’ 。愛掏網 - it200.com
8. 在您的應用程序的.gradle文件中添加firebase SDK依賴項,并在IDE中點擊“立即同步”并點擊 “下一步” 。愛掏網 - it200.com
build.gradle(項目)
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.google.gms:google-services:4.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
build.gradle(模塊)
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.firebase:firebase-auth:16.0.3'
implementation 'com.google.firebase:firebase-core:16.0.3'
implementation 'com.google.android.gms:play-services-auth:16.0.0'
implementation 'com.github.bumptech.glide:glide:3.7.0'
}
apply plugin: 'com.google.gms.google-services'
AndroidManifest.xml
在AndroidManifest.xml文件中添加 Internet 權限以訪問網絡連接。愛掏網 - it200.com
<uses-permission android:name="android.permission.INTERNET" />
9. 運行您的應用程序以驗證安裝配置,如果一切正常,它將顯示一個成功消息,然后點擊 ‘繼續到控制臺’ 。愛掏網 - it200.com
10. 在控制臺頁面上選擇 認證 -gt; 登錄方法 -gt; Google -gt; 啟用 并點擊 ‘保存’ 。愛掏網 - it200.com
Android Firebase身份驗證與Google登錄示例
在這個示例中,我們將在我們的Android應用程序中集成Firebase身份驗證與Google登錄。愛掏網 - it200.com一旦用戶通過Google登錄成功登錄,我們將使用Firebase身份驗證對他們進行身份驗證,然后將用戶重定向到下一個活動(ProfileActivity)并顯示他們的詳細信息。愛掏網 - it200.com
我們需要將下載的 ‘google-services.json’ 文件粘貼到我們的Android項目應用程序目錄中。愛掏網 - it200.com
activity_main.xml
在activity_main.xml文件中添加以下代碼。愛掏網 - it200.com在這個活動中,我們實現了自定義的Google登錄按鈕。愛掏網 - it200.com
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="example.javatpoint.com.firebasegooglelogin.MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textSize="20dp"
android:text="This is main activity, sign in to move next activity." />
<com.google.android.gms.common.SignInButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/sign_in_button"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp">
</com.google.android.gms.common.SignInButton>
</RelativeLayout>
strings.xml
在string.xml文件中添加您的Web客戶端ID。愛掏網 - it200.com您也可以在程序中使用默認的Google Web客戶端ID。愛掏網 - it200.com
<resources>
<string name="app_name">Firebase Google Login</string>
<string name="web_client_id">xxxxxxxx..place your web client id here</string>
</resources>
MainActivity.java
在MainActivity.java類中添加以下代碼。愛掏網 - it200.comMainActivity.java類中使用的類和方法的功能如下:
- 使用 new FirebaseAuth.AuthStateListener() 作為身份驗證偵聽器,檢查用戶是否登入。愛掏網 - it200.com如果用戶成功登入,則執行其他Firebase邏輯。愛掏網 - it200.com
- 使用 new GoogleSignInOptions.Builder() 方法配置Google Sign-in,使用requestEmail選項獲取用戶數據。愛掏網 - it200.com
- 點擊“登錄”按鈕時,調用 Auth.GoogleSignInApi.getSignInIntent(googleApiClient) ,并通過startActivityForResult()啟動意圖。愛掏網 - it200.com
- 在 onActivityResult() 中獲取Google Sign-in請求的結果。愛掏網 - it200.com
- 如果 handleSignInResult(result) 返回true,則使用idToken獲取Google認證。愛掏網 - it200.com
- 調用 firebaseAuthWithGoogle(credential) 與Google進行Firebase身份驗證。愛掏網 - it200.com
如果Firebase身份驗證成功,我們將用戶重定向到下一個活動(ProfileActivity.java)。愛掏網 - it200.com
package example.javatpoint.com.firebasegooglelogin;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GoogleAuthProvider;
public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = "MainActivity";
private SignInButton signInButton;
private GoogleApiClient googleApiClient;
private static final int RC_SIGN_IN = 1;
String name, email;
String idToken;
private FirebaseAuth firebaseAuth;
private FirebaseAuth.AuthStateListener authStateListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firebaseAuth = com.google.firebase.auth.FirebaseAuth.getInstance();
//this is where we start the Auth state Listener to listen for whether the user is signed in or not
authStateListener = new FirebaseAuth.AuthStateListener(){
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
// Get signedIn user
FirebaseUser user = firebaseAuth.getCurrentUser();
//if user is signed in, we call a helper method to save the user details to Firebase
if (user != null) {
// User is signed in
// you could place other firebase code
//logic to save the user details to Firebase
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
}
};
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.web_client_id))//you can also use R.string.default_web_client_id
.requestEmail()
.build();
googleApiClient=new GoogleApiClient.Builder(this)
.enableAutoManage(this,this)
.addApi(Auth.GOOGLE_SIGN_IN_API,gso)
.build();
signInButton = findViewById(R.id.sign_in_button);
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
startActivityForResult(intent,RC_SIGN_IN);
}
});
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==RC_SIGN_IN){
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
private void handleSignInResult(GoogleSignInResult result){
if(result.isSuccess()){
GoogleSignInAccount account = result.getSignInAccount();
idToken = account.getIdToken();
name = account.getDisplayName();
email = account.getEmail();
// you can store user data to SharedPreference
AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null);
firebaseAuthWithGoogle(credential);
}else{
// Google Sign In failed, update UI appropriately
Log.e(TAG, "Login Unsuccessful. "+result);
Toast.makeText(this, "Login Unsuccessful", Toast.LENGTH_SHORT).show();
}
}
private void firebaseAuthWithGoogle(AuthCredential credential){
firebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
if(task.isSuccessful()){
Toast.makeText(MainActivity.this, "Login successful", Toast.LENGTH_SHORT).show();
gotoProfile();
}else{
Log.w(TAG, "signInWithCredential" + task.getException().getMessage());
task.getException().printStackTrace();
Toast.makeText(MainActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
private void gotoProfile(){
Intent intent = new Intent(MainActivity.this, ProfileActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
@Override
protected void onStart() {
super.onStart();
if (authStateListener != null){
FirebaseAuth.getInstance().signOut();
}
firebaseAuth.addAuthStateListener(authStateListener);
}
@Override
protected void onStop() {
super.onStop();
if (authStateListener != null){
firebaseAuth.removeAuthStateListener(authStateListener);
}
}
}
profile_activity.xml
將以下代碼添加到profile_activity.xml文件中。愛掏網 - it200.comImageView用于顯示用戶個人資料圖像,TextView用于顯示姓名、電子郵件、ID,按鈕用于注銷。愛掏網 - it200.com
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="example.javatpoint.com.firebasegooglelogin.ProfileActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/profileImage"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"
android:text="name"
android:textSize="20dp"
android:layout_marginTop="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/email"
android:textSize="20dp"
android:text="email"
android:layout_marginTop="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/userId"
android:textSize="20dp"
android:text="id"
android:layout_marginTop="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/logoutBtn"
android:text="Logout"
android:layout_marginTop="30dp"/>
</LinearLayout>
</RelativeLayout>
ProfileActivity.java
在這個ProfileActivity.java類中,我們獲取用戶信息并顯示。愛掏網 - it200.comGoogleSignInResult類實現了Result接口,表示調用Google Play服務的API方法后的最終結果。愛掏網 - it200.com
GoogleSignInAccount類保存用戶的必要信息。愛掏網 - it200.com
package example.javatpoint.com.firebasegooglelogin;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.OptionalPendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.firebase.auth.FirebaseAuth;
public class ProfileActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener {
Button logoutBtn;
TextView userName,userEmail,userId;
ImageView profileImage;
private GoogleApiClient googleApiClient;
private GoogleSignInOptions gso;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
logoutBtn = findViewById(R.id.logoutBtn);
userName = findViewById(R.id.name);
userEmail = findViewById(R.id.email);
userId = findViewById(R.id.userId);
profileImage = findViewById(R.id.profileImage);
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
googleApiClient=new GoogleApiClient.Builder(this)
.enableAutoManage(this,this)
.addApi(Auth.GOOGLE_SIGN_IN_API,gso)
.build();
logoutBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FirebaseAuth.getInstance().signOut();
Auth.GoogleSignInApi.signOut(googleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (status.isSuccess()){
gotoMainActivity();
}else{
Toast.makeText(getApplicationContext(),"Session not close",Toast.LENGTH_LONG).show();
}
}
});
}
});
}
@Override
protected void onStart() {
super.onStart();
OptionalPendingResult<GoogleSignInResult> opr= Auth.GoogleSignInApi.silentSignIn(googleApiClient);
if(opr.isDone()){
GoogleSignInResult result=opr.get();
handleSignInResult(result);
}else{
opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(@NonNull GoogleSignInResult googleSignInResult) {
handleSignInResult(googleSignInResult);
}
});
}
}
private void handleSignInResult(GoogleSignInResult result){
if(result.isSuccess()){
GoogleSignInAccount account=result.getSignInAccount();
userName.setText(account.getDisplayName());
userEmail.setText(account.getEmail());
userId.setText(account.getId());
try{
Glide.with(this).load(account.getPhotoUrl()).into(profileImage);
}catch (NullPointerException e){
Toast.makeText(getApplicationContext(),"image not found",Toast.LENGTH_LONG).show();
}
}else{
gotoMainActivity();
}
}
private void gotoMainActivity(){
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
輸出: