您可以將網頁式一般 OAuth 登入機制整合至應用程式,並使用 Firebase SDK 執行端對端登入流程,讓使用者透過 Yahoo 等 OAuth 供應商,使用 Firebase 進行驗證。
事前準備
如要讓使用者透過 Yahoo 帳戶登入,您必須先在 Firebase 專案中啟用 Yahoo 做為登入供應商:
- 在 Firebase 控制台中,依序前往 「Security」(安全性) >「Authentication」(驗證)。
- 在「登入方式」分頁中,啟用「Yahoo」登入供應商。
-
將該供應商開發人員控制台中的「Client ID」(用戶端 ID) 和「Client Secret」(用戶端密鑰) 新增至供應商設定:
-
如要註冊 Yahoo OAuth 用戶端,請按照 Yahoo 開發人員說明文件中的向 Yahoo 註冊網頁應用程式操作。
請務必選取兩個 OpenID Connect API 權限:
profile和email。 -
向這些供應商註冊應用程式時,請務必將專案的
*.firebaseapp.com網域註冊為應用程式的重新導向網域。
-
- 按一下「儲存」。
指定應用程式的 SHA-1 指紋 (如果尚未指定)。
在 Firebase 控制台中,依序前往「
Settings 」(設定) >「General」(一般) 分頁。向下捲動至「您的應用程式」資訊卡,選取 Android 應用程式,然後在「SHA 憑證指紋」欄位中新增 SHA-1 指紋。
如要瞭解如何取得應用程式的 SHA 指紋,請參閱「驗證用戶端」。
使用 Firebase SDK 處理登入流程
如果您正在建構 Android 應用程式,要使用 Yahoo 帳戶透過 Firebase 驗證使用者,最簡單的方法是使用 Firebase Android SDK 處理整個登入流程。
如要使用 Firebase Android SDK 處理登入流程,請按照下列步驟操作:
使用 OAuthProvider 的 Builder 建構執行個體,並將供應商 ID 設為 yahoo.com。
Kotlin
val provider = OAuthProvider.newBuilder("yahoo.com")
Java
OAuthProvider.Builder provider = OAuthProvider.newBuilder("yahoo.com");
選用:指定要與 OAuth 要求一併傳送的其他自訂 OAuth 參數。
Kotlin
// Prompt user to re-authenticate to Yahoo. provider.addCustomParameter("prompt", "login") // Localize to French. provider.addCustomParameter("language", "fr")
Java
// Prompt user to re-authenticate to Yahoo. provider.addCustomParameter("prompt", "login"); // Localize to French. provider.addCustomParameter("language", "fr");
如要瞭解 Yahoo 支援的參數,請參閱 Yahoo OAuth 說明文件。請注意,您無法使用
setCustomParameters()傳遞 Firebase 必要的參數。這些參數包括 client_id、redirect_uri、response_type、scope 和 state。選用:指定要向驗證服務供應商要求的其他 OAuth 2.0 範圍 (
profile和email以外)。如果應用程式需要透過 Yahoo API 存取私人使用者資料,您必須在 Yahoo 開發人員控制台的「API Permissions」下方,要求 Yahoo API 權限。要求的 OAuth 範圍必須與應用程式 API 權限中預先設定的範圍完全相符。舉例來說,如果要求使用者聯絡人的讀取/寫入權限,且已在應用程式的 API 權限中預先設定,則必須傳遞sdct-w,而非唯讀 OAuth 範圍sdct-r。否則流程會失敗,使用者也會看到錯誤訊息。Kotlin
// Request read access to a user's email addresses. // This must be preconfigured in the app's API permissions. provider.scopes = listOf("mail-r", "sdct-w")
Java
詳情請參閱 Yahoo 範圍說明文件。// Request read access to a user's email addresses. // This must be preconfigured in the app's API permissions. List<String> scopes = new ArrayList<String>() { { // Request access to Yahoo Mail API. add("mail-r"); // This must be preconfigured in the app's API permissions. add("sdct-w"); } }; provider.setScopes(scopes);
使用 OAuth 供應商物件向 Firebase 驗證。請注意,與其他 FirebaseAuth 作業不同,這項作業會彈出自訂 Chrome 分頁,接管使用者介面。因此,請勿在附加的 OnSuccessListeners 和 OnFailureListeners 中參照 Activity,因為作業啟動 UI 時,這些事件監聽器會立即卸載。
請先確認是否已收到回覆。透過這個方法登入會將活動置於背景,這表示系統可能會在登入流程中回收活動。為確保使用者在這種情況下不必重試,請檢查結果是否已存在。
如要檢查是否有待處理的結果,請呼叫
getPendingAuthResult:Kotlin
val pendingResultTask = firebaseAuth.pendingAuthResult if (pendingResultTask != null) { // There's something already here! Finish the sign-in for your user. pendingResultTask .addOnSuccessListener { // User is signed in. // IdP data available in // authResult.getAdditionalUserInfo().getProfile(). // The OAuth access token can also be retrieved: // ((OAuthCredential)authResult.getCredential()).getAccessToken(). // The OAuth secret can be retrieved by calling: // ((OAuthCredential)authResult.getCredential()).getSecret(). } .addOnFailureListener { // Handle failure. } } else { // There's no pending result so you need to start the sign-in flow. // See below. }
Java
Task<AuthResult> pendingResultTask = firebaseAuth.getPendingAuthResult(); if (pendingResultTask != null) { // There's something already here! Finish the sign-in for your user. pendingResultTask .addOnSuccessListener( new OnSuccessListener<AuthResult>() { @Override public void onSuccess(AuthResult authResult) { // User is signed in. // IdP data available in // authResult.getAdditionalUserInfo().getProfile(). // The OAuth access token can also be retrieved: // ((OAuthCredential)authResult.getCredential()).getAccessToken(). // The OAuth secret can be retrieved by calling: // ((OAuthCredential)authResult.getCredential()).getSecret(). } }) .addOnFailureListener( new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Handle failure. } }); } else { // There's no pending result so you need to start the sign-in flow. // See below. }
如要啟動登入流程,請呼叫
startActivityForSignInWithProvider:Kotlin
firebaseAuth .startActivityForSignInWithProvider(activity, provider.build()) .addOnSuccessListener { // User is signed in. // IdP data available in // authResult.getAdditionalUserInfo().getProfile(). // The OAuth access token can also be retrieved: // ((OAuthCredential)authResult.getCredential()).getAccessToken(). // The OAuth secret can be retrieved by calling: // ((OAuthCredential)authResult.getCredential()).getSecret(). } .addOnFailureListener { // Handle failure. }
Java
firebaseAuth .startActivityForSignInWithProvider(/* activity= */ this, provider.build()) .addOnSuccessListener( new OnSuccessListener<AuthResult>() { @Override public void onSuccess(AuthResult authResult) { // User is signed in. // IdP data available in // authResult.getAdditionalUserInfo().getProfile(). // The OAuth access token can also be retrieved: // ((OAuthCredential)authResult.getCredential()).getAccessToken(). // The OAuth secret can be retrieved by calling: // ((OAuthCredential)authResult.getCredential()).getSecret(). } }) .addOnFailureListener( new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Handle failure. } });
成功完成後,您可以從傳回的
OAuthCredential物件中,擷取與提供者相關聯的 OAuth 存取權杖。您可以使用 OAuth 存取權杖呼叫 Yahoo API。
其中
YAHOO_USER_UID是 Yahoo 使用者 ID,可從firebaseAuth.getCurrentUser().getProviderData().get(0).getUid()欄位或authResult.getAdditionalUserInfo().getProfile()剖析。雖然上述範例著重於登入流程,但您也可以使用
startActivityForLinkWithProvider將 Yahoo 供應商連結至現有使用者。舉例來說,您可以將多個供應商連結至同一位使用者,讓他們能使用任一供應商登入。Kotlin
// The user is already signed-in. val firebaseUser = firebaseAuth.currentUser!! firebaseUser .startActivityForLinkWithProvider(activity, provider.build()) .addOnSuccessListener { // Provider credential is linked to the current user. // IdP data available in // authResult.getAdditionalUserInfo().getProfile(). // The OAuth access token can also be retrieved: // authResult.getCredential().getAccessToken(). // The OAuth secret can be retrieved by calling: // authResult.getCredential().getSecret(). } .addOnFailureListener { // Handle failure. }
Java
// The user is already signed-in. FirebaseUser firebaseUser = firebaseAuth.getCurrentUser(); firebaseUser .startActivityForLinkWithProvider(/* activity= */ this, provider.build()) .addOnSuccessListener( new OnSuccessListener<AuthResult>() { @Override public void onSuccess(AuthResult authResult) { // Provider credential is linked to the current user. // IdP data available in // authResult.getAdditionalUserInfo().getProfile(). // The OAuth access token can also be retrieved: // authResult.getCredential().getAccessToken(). // The OAuth secret can be retrieved by calling: // authResult.getCredential().getSecret(). } }) .addOnFailureListener( new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Handle failure. } });
startActivityForReauthenticateWithProvider也可以使用相同的模式,用於擷取需要近期登入的機密作業的新憑證。Kotlin
// The user is already signed-in. val firebaseUser = firebaseAuth.currentUser!! firebaseUser .startActivityForReauthenticateWithProvider(activity, provider.build()) .addOnSuccessListener { // User is re-authenticated with fresh tokens and // should be able to perform sensitive operations // like account deletion and email or password // update. } .addOnFailureListener { // Handle failure. }
Java
// The user is already signed-in. FirebaseUser firebaseUser = firebaseAuth.getCurrentUser(); firebaseUser .startActivityForReauthenticateWithProvider(/* activity= */ this, provider.build()) .addOnSuccessListener( new OnSuccessListener<AuthResult>() { @Override public void onSuccess(AuthResult authResult) { // User is re-authenticated with fresh tokens and // should be able to perform sensitive operations // like account deletion and email or password // update. } }) .addOnFailureListener( new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Handle failure. } });
後續步驟
使用者首次登入後,系統會建立新的使用者帳戶,並連結至使用者登入時使用的憑證,也就是使用者名稱和密碼、電話號碼或驗證提供者資訊。這個新帳戶會儲存為 Firebase 專案的一部分,可用於識別專案中所有應用程式的使用者,無論使用者如何登入。
-
在應用程式中,您可以從
FirebaseUser物件取得使用者的基本個人資料。請參閱「管理使用者」。 在 Firebase Realtime Database 和 Cloud Storage安全性規則中,您可以從
auth變數取得已登入使用者的專屬使用者 ID,並用來控管使用者可存取的資料。
您可以將驗證供應商憑證連結至現有使用者帳戶,允許使用者透過多個驗證供應商登入應用程式。
如要登出使用者,請呼叫 signOut:
Kotlin
Firebase.auth.signOut()
Java
FirebaseAuth.getInstance().signOut();