如要將 Google 登入功能整合至您的 Android 應用程式,請設定 Google 登入,然後在應用程式版面配置中加入按鈕,以啟動登入流程。
事前準備
設定 Google API 控制台專案並設定 Android Studio 專案。
設定 Google 登入和 GoogleSignInClient 物件
在登入活動的
onCreate
方法中,設定 Google 登入以要求應用程式所需的使用者資料。例如,設定 Google 登入以要求使用者 ID 和基本設定檔資訊時,請使用DEFAULT_SIGN_IN
參數建立GoogleSignInOptions
物件。如要一併要求使用者的電子郵件地址,請使用requestEmail
選項建立GoogleSignInOptions
物件。// 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();
如果需要要求其他範圍來存取 Google API,請使用
requestScopes
指定。為了提供最佳使用者體驗,請在登入時,只要求應用程式需要的最小範圍。只在您需要時才要求其他範圍,以便使用者能在執行操作後看到同意畫面。請參閱要求其他範圍。然後在登入活動的
onCreate
方法中,以您指定的選項建立GoogleSignInClient
物件。// Build a GoogleSignInClient with the options specified by gso. mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
檢查目前登入的使用者
在活動的 onStart
方法中,檢查使用者是否已使用 Google 帳戶登入您的應用程式。
// 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);
如果 GoogleSignIn.getLastSignedInAccount
回傳 GoogleSignInAccount
物件 (而非 null
),使用者已透過 Google 登入您的應用程式。視情況更新您的使用者介面,也就是隱藏登入按鈕、啟動主要活動,或是任何適合您應用程式的用途。
如果 GoogleSignIn.getLastSignedInAccount
傳回 null
,表示使用者尚未使用 Google 登入您的應用程式。更新您的使用者介面,以顯示 Google 登入按鈕。
在應用程式中加入 Google 登入按鈕
在應用程式的版面配置中加入
SignInButton
:<com.google.android.gms.common.SignInButton android:id="@+id/sign_in_button" android:layout_width="wrap_content" android:layout_height="wrap_content" />
選用:如果您使用的是預設登入按鈕圖片,而非提供自己的登入按鈕素材資源,則可以使用
setSize
方法自訂按鈕的大小。// Set the dimensions of the sign-in button. SignInButton signInButton = findViewById(R.id.sign_in_button); signInButton.setSize(SignInButton.SIZE_STANDARD);
在 Android 活動 (例如
onCreate
方法中),註冊按鈕的OnClickListener
以便在使用者點擊時登入:findViewById(R.id.sign_in_button).setOnClickListener(this);
啟動登入流程
在活動的
onClick
方法中,使用getSignInIntent
方法建立登入意圖,並使用startActivityForResult
啟動意圖,以處理登入按鈕的輕觸動作。@Override public void onClick(View v) { switch (v.getId()) { case R.id.sign_in_button: signIn(); break; // ... } }
private void signIn() { Intent signInIntent = mGoogleSignInClient.getSignInIntent(); startActivityForResult(signInIntent, RC_SIGN_IN); }
啟動意圖會提示使用者選取要用來登入的 Google 帳戶。如果您要求的範圍超過
profile
、email
和openid
,系統會提示使用者授予要求權限。使用者登入後,您可以在活動的
onActivityResult
方法中取得使用者的GoogleSignInAccount
物件。@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); } }
GoogleSignInAccount
物件包含已登入使用者的相關資訊,例如使用者名稱。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()); updateUI(null); } }
您也可以用
getEmail
取得使用者的電子郵件地址,用於 Google 的用戶端 ID (用於用戶端使用) 和getId
,以及使用者的 ID 憑證 (使用getIdToken
)。如果您需要將目前登入的使用者傳送至後端伺服器,請將 ID 憑證傳送至您的後端伺服器,並在伺服器上驗證該憑證。