

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 搭配使用者集區使用社交身分提供者
<a name="cognito-user-pools-social-idp"></a>

您的 Web 和行動應用程式使用者可透過社交身分提供者 (IdP，如 Facebook、Google、Amazon 或 Apple) 進行登入。Amazon Cognito 使用內建的託管 Web UI，可以為所有已進行身分驗證的使用者提供字符處理和管理。這樣，您的後端系統可以對一組使用者集區字符進行標準化。您必須啟用受管登入，才能與支援的社交身分提供者整合。當 Amazon Cognito 建置您的受管登入頁面時，它會建立 Amazon Cognito 和 OIDC 和社交 IdP 用來交換資訊的 OAuth 2.0 端點。 Amazon Cognito IdPs 如需詳細資訊，請參閱 [Amazon Cognito 使用者集區 Auth API 參考](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-userpools-server-contract-reference.html)。

您可以在 中新增社交 IdP AWS 管理主控台，也可以使用 AWS CLI 或 Amazon Cognito API。

**注意**  
Amazon Cognito 使用者集區提供透過第三方 (聯合身分) 登入。這項功能與透過 Amazon Cognito 身分集區 (聯合身分) 登入無關。

**Topics**
+ [設定社交 IdP 開發人員帳戶和應用程式](#cognito-user-pools-social-idp-step-1)
+ [使用社交 IdP 設定您的使用者集區](#cognito-user-pools-social-idp-step-2)
+ [測試社交 IdP 組態](#cognito-user-pools-social-idp-step-3)

## 設定社交 IdP 開發人員帳戶和應用程式
<a name="cognito-user-pools-social-idp-step-1"></a>

與 Amazon Cognito 建立社交 IdP 之前，您必須將您的應用程式註冊至社交 IdP 以接收用戶端 ID 和用戶端密碼。

------
#### [ Facebook ]

如需有關中繼開發人員帳戶和身分驗證組態的最新資訊，請參閱[中繼應用程式開發](https://developers.facebook.com/docs/development)。

**如何向 Facebook/Meta 註冊應用程式**

1. [向 Facebook 建立開發人員帳戶](https://developers.facebook.com/docs/facebook-login)。

1. 使用您的 Facebook 憑證[登入](https://developers.facebook.com/)。

1. 從 **My Apps** (我的應用程式) 選單中，選擇 **Create New App** (建立新的應用程式)。

1. 輸入您 Facebook 應用程式的名稱，然後選擇 **Create App ID** (建立應用程式 ID)。

1. 在左側導覽列中，選擇 **Settings** (設定) 然後 **Basic** (基本)。

1. 記下 **App ID (應用程式 ID)** 和 **App Secret (應用程式秘密)**。您會在下一節中用到它們。

1. 從頁面底部選擇 **\+ Add Platform** (\+ 新增平台)。

1. 選擇 **Website** (網站)。

1. 在 **Website** (網站) 下，請將您應用程式登入頁面的路徑輸入 **Site URL** (網站 URL)。

   ```
   https://{{mydomain.auth.us-east-1.amazoncognito.com}}/login?response_type=code&client_id={{1example23456789}}&redirect_uri={{https://www.example.com}}
   ```

1. 選擇**儲存變更**。

1. 將您的使用者集區網域的根路徑輸入 **App Domains** (應用程式網域)。

   ```
   https://{{mydomain.auth.us-east-1.amazoncognito.com}}
   ```

1. 選擇**儲存變更**。

1. 從導覽列選擇 **Add Product** (新增產品)，並為 **Facebook Login** (Facebook 登入) 產品選擇 **Set up** (設定)。

1. 從導覽列選擇 **Facebook Login** (Facebook 登入)，然後選擇 **Settings** (設定)。

   將您使用者集區網域的 `/oauth2/idpresponse` 端點路徑輸入至 **Valid OAuth Redirect URIs (有效的 OAuth 重新導向 URI)**。

   ```
   https://{{mydomain.auth.us-east-1.amazoncognito.com}}/oauth2/idpresponse
   ```

1. 選擇**儲存變更**。

------
#### [ Login with Amazon ]

如需 Login with Amazon 開發人員帳戶和身分驗證組態的最新資訊，請參閱 [Login with Amazon 文件](https://developer.amazon.com/docs/login-with-amazon/documentation-overview.html)。

**如何使用 Login with Amazon 註冊應用程式**

1. [向 Amazon 建立開發人員帳戶](https://developer.amazon.com/login-with-amazon)。

1. 使用您的 Amazon 憑證[登入](https://developer.amazon.com/lwa/sp/overview.html)。

1. 您需要建立 Amazon 安全性設定檔以便接收 Amazon 用戶端 ID 和用戶端秘密。

   從頁面頂端的導覽列中選擇 **Apps and Services (應用程式和服務)**，然後選擇 **Login with Amazon** (登入 Amazon)。

1. 選擇 **Create a Security Profile** (建立安全性設定檔)。

1. 輸入 **Security Profile Name** (安全設定檔名稱)、**Security Profile Description** (安全設定檔描述) 和 **Consent Privacy Notice URL** (同意隱私權聲明 URL)。

1. 選擇**儲存**。

1. 選擇 **Client ID** (用戶端 ID) 和 **Client Secret** (用戶端秘密)，以顯示用戶端 ID 和秘密。您會在下一節中用到它們。

1. 將滑鼠移到齒輪圖示上方並選擇 **Web Settings** (Web 設定)，然後選擇 **Edit** (編輯)。

1. 在 **Allowed Origins** (允許的來源) 中輸入您的使用者集區網域。

   ```
   https://{{mydomain.auth.us-east-1.amazoncognito.com}}
   ```

1. 在 **Allowed Return URLs** (允許的傳回 URL) 中輸入具有 `/oauth2/idpresponse` 端點的使用者集區網域。

   ```
   https://{{mydomain.auth.us-east-1.amazoncognito.com}}/oauth2/idpresponse
   ```

1. 選擇**儲存**。

------
#### [ Google ]

如需 Google Cloud Platform 中 OAuth 2.0 的詳細資訊，請參閱 Google Workspace for Developers 文件中的[瞭解身分驗證和授權](https://developers.google.com/workspace/guides/auth-overview)。

**如何向 Google 註冊應用程式**

1. [向 Google 建立開發人員帳戶](https://developers.google.com/identity)。

1. 登入 [Google Cloud Platform 主控台](https://console.cloud.google.com/home/dashboard)。

1. 在頂端導覽列中，選擇 **Select a project** (選取專案)。如果您在 Google 平台中已經有專案，則此功能表將改為顯示您的預設專案。

1. 選擇 **NEW PROJECT (新專案)**。

1. 輸入專案的名稱，然後選擇 **CREATE (建立)**。

1. 在左側導覽列中，選擇 **APIs and Services** (API 和服務)，然後選擇 **OAuth consent screen** (OAuth 同意畫面)。

1. 輸入應用程式資訊，**App domain** (應用程式網域)、**Authorized domains** (授權網域)，以及 **Developer contact information** (開發人員聯絡資訊)。您的 **Authorized domains** (授權網域) 必須包含您自訂網域的 `amazoncognito.com` 與根目錄，例如 `example.com`。選擇 **SAVE AND CONTINUE (儲存並繼續)**。

1. 1. 在 **Scopes** (範圍) 下方，選擇 **Add or remove scopes** (新增或移除範圍)，然後至少選擇下列 OAuth 範圍。

   1. `.../auth/userinfo.email`

   1. `.../auth/userinfo.profile`

   1. openid

1. 在 **Test users** (測試使用者) 下方，選擇 **Add users** (新增使用者)。輸入您的電子郵件地址和任何其他授權的測試使用者，然後選擇 **SAVE AND CONTINUE** (儲存並繼續)。

1. 再次展開左側導覽列，然後依序選擇 **APIs and Services** (API 和服務)、**Credentials** (憑證)。

1. 依序選擇 **CREATE CREDENTIALS** (建立憑證)、**OAuth client ID** (OAuth 用戶端 ID)。

1. 選擇 **Application type** (應用程式類型)，並為您的用戶端取一個 **Name** (名稱)。

1. 在 **Authorized JavaScript origins** (授權的 JavaScript 來源) 下方，選擇 **ADD URI** (新增 URI)。輸入您的使用者集區網域。

   ```
   https://{{mydomain.auth.us-east-1.amazoncognito.com}}
   ```

1. 在 **Authorized redirect URIs** (授權的重新導向 URI) 下方，選擇 **ADD URI** (新增 URI)。輸入您使用者集區網域的 `/oauth2/idpresponse` 端點路徑。

   ```
   https://{{mydomain.auth.us-east-1.amazoncognito.com}}/oauth2/idpresponse
   ```

1. 選擇 **CREATE** (建立)。

1. 安全地儲存 Google 在 **Your client ID** (您的用戶端 ID) 和 **Your client secret** (您的用戶端密碼) 下顯示的值。當您新增 Google IdP 時，請將這些值提供給 Amazon Cognito。

------
#### [ Sign in with Apple ]

如需設定「使用 Apple 登入」的最新資訊，請參閱《Apple 開發人員文件》中的[設定您的環境以使用 Apple 登入](https://developer.apple.com/documentation/signinwithapple/configuring-your-environment-for-sign-in-with-apple)。

**如何使用 Sign in with Apple (SIWA) 註冊應用程式**

1. [向 Apple 建立開發人員帳戶](https://developer.apple.com/programs/enroll/)。

1. 使用您的 Apple 憑證[登入](https://developer.apple.com/account/#/welcome)。

1. 在左側導覽列上，選擇 **Certificates, Identifiers & Profiles (憑證、識別碼與設定檔)**。

1. 在左側導覽列上，選擇 **Identifiers** (識別碼)。

1. 在 **Identifiers (識別碼)** 頁面上，選擇 **\+** 圖示。

1. 在 **Register a New Identifier** (註冊新的識別碼) 頁面上，選擇 **App IDs** (應用程式 ID)，然後選擇 **Continue** (繼續)。

1. 在 **Select a type (選取類型)** 頁面上，選擇 **App (應用程式)**，然後選擇 **Continue (繼續)**。

1. 在 **Register an App ID** (註冊應用程式 ID) 頁面上，執行下列操作：

   1. 在 **Description** (描述) 下方輸入描述。

   1. 在 **App ID Prefix** (應用程式 ID 字首) 下方，輸入 **Bundle ID** (套件 ID)。記下 **App ID Prefix** (應用程式 ID 字首) 下的值。在 [使用社交 IdP 設定您的使用者集區](#cognito-user-pools-social-idp-step-2) 中選擇 Apple 作為您的身分提供者之後，您將會使用此值。

   1. 在 **Capabilities** (功能) 下方，選擇 **Sign In with Apple**，然後選擇 **Edit** (編輯)。

   1. 在 **Sign in with Apple: App ID Configuration** (Sign in with Apple：應用程式 ID 組態) 頁面上，選擇將應用程式設定為主要應用程式或與其他應用程式 ID 群組，然後選擇 **Save** (儲存)。

   1. 選擇**繼續**。

1. 在 **Confirm your App ID** (確認您的應用程式 ID) 頁面上，選擇 **Register** (註冊)。

1. 在 **Identifiers** (識別碼) 頁面上，選擇 **\+** 圖示。

1. 在 **Register a New Identifier** (註冊新的識別碼) 頁面上，選擇 **Services IDs** (服務 ID)，然後選擇 **Continue** (繼續)。

1. 在 **Register a Services ID** (註冊服務 ID) 頁面上，執行下列操作：

   1. 在 **Description** (說明) 下方，輸入說明內容。

   1. 在 **Identifier** (識別碼) 下方，輸入識別碼。記下服務 ID，因為在 [使用社交 IdP 設定您的使用者集區](#cognito-user-pools-social-idp-step-2) 中選擇 Apple 當作您的身分提供者後，您會需要此值。

   1. 選擇 **Continue (繼續)**，然後選擇 **Register (註冊)**。

1. 從「Identifiers (識別碼)」頁面中選擇您剛剛建立的服務 ID。

   1. 選取 **Sign In with Apple**，然後選擇 **Configure** (設定)。

   1. 在 **Web Authentication Configuration** (Web 身分驗證組態) 頁面上，選取您稍早建立的應用程式 ID 作為 **Primary App ID** (主要應用程式 ID)。

   1. 在 **Website URLs (網站 URL)** 旁邊選擇 **\+** 圖示。

   1. 在 **Domains and subdomains** (網域與子網域) 下方，輸入不含 `https://` 字首的使用者集區網域。

      ```
      {{mydomain.auth.us-east-1.amazoncognito.com}}
      ```

   1. 在 **Return URLs (傳回 URL)** 下，輸入您使用者集區網域的 `/oauth2/idpresponse` 端點路徑。

      ```
      https://{{mydomain.auth.us-east-1.amazoncognito.com}}/oauth2/idpresponse
      ```

   1. 選擇 **Next** (下一步)，然後選擇 **Done** (完成)。您不須驗證網域。

   1. 選擇 **Continue (繼續)**，然後選擇 **Save (儲存)**。

1. 在左側導覽列上，選擇 **Keys** (金鑰)。

1. 在 **Keys** (金鑰) 頁面上，選擇 **\+** 圖示。

1. 在 **Register a New Key** (註冊新的金鑰) 頁面上，執行下列操作：

   1. 在 **Key Name** (金鑰名稱) 下，輸入金鑰名稱。

   1. 選擇 **Sign In with Apple**，然後選擇 **Configure** (設定)。

   1. 在 **Configure Key** (設定金鑰) 頁面上，然後選取您稍早建立的應用程式 ID 作為 **Primary App ID** (主要應用程式 ID)。選擇**儲存**。

   1. 選擇 **Continue** (繼續)，然後選擇 **Register** (註冊)。

1. 在 **Download Your Key** (下載您的金鑰) 頁面，選擇 **Download** (下載) 以下載私有金鑰，記下顯示的 **Key ID** (金鑰 ID)，接著選擇 **Done** (完成)。您在 [使用社交 IdP 設定您的使用者集區](#cognito-user-pools-social-idp-step-2) 中選擇 Apple 當作身分提供者後，需有此頁面上顯示的私有金鑰和 **Key ID (金鑰 ID)** 值。

------

## 使用社交 IdP 設定您的使用者集區
<a name="cognito-user-pools-social-idp-step-2"></a>

**使用 設定使用者集區社交 IdP AWS 管理主控台**

1. 前往 [Amazon Cognito 主控台](https://console.aws.amazon.com/cognito/home)。如果出現提示，請輸入您的 AWS 登入資料。

1. 選擇 **User Pools** (使用者集區)。

1. 從清單中選擇現有的使用者集區，或建立使用者集區。

1. 選擇**社交和外部提供者**選單，然後選取**新增身分提供者**。

1. 選擇社交 IdP：**Facebook**、**Google**、**Login with Amazon** (登入 Amazon) 或 **Sign in with Apple** (使用 Apple 登入)。

1. 根據您選擇的社交 IdP，從以下步驟中選擇：
   + **Google** 和 **Login with Amazon** (登入 Amazon)– 輸入上一節產生的 **app client ID** (應用程式用戶端 ID) 和 **app client secret** (應用程式用戶端密碼)。
   + **Facebook** – 輸入上一節產生的 **app client ID** (應用程式用戶端 ID) 和 **app client secret** (應用程式用戶端秘密)，然後選擇 API 版本 (例如版本 2.12)。建議您選擇最新可用的版本，因為每個 Facebook API 都有生命週期和停止支援日期。Facebook 範圍和屬性可能隨 API 版本而有所不同。建議您使用 Facebook 測試您的社交身分登入，以確保聯合使用正常運作。
   + **Sign In with Apple** – 輸入上一節產生的 **Services ID** (服務 ID)、**Team ID** (團隊 ID)、**Key ID** (金鑰 ID) 和 **private key** (私有金鑰)。

1. 輸入您要使用的**授權範圍**名稱。範圍可定義您要透過應用程式來存取的使用者屬性 (例如 `name` 和 `email`)。若為 Facebook，這些屬性應以逗號分隔。若為 Google 和登入 Amazon，則應以空格分隔。若為 Sign in with Apple，請針對您想要存取的範圍勾選核取方塊。    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/cognito/latest/developerguide/cognito-user-pools-social-idp.html)

   會向您的應用程式使用者提示同意提供這些屬性給應用程式。如需社交供應商範圍的詳細資訊，請參閱 Google、Facebook、Login with Amazon 或 Sign in with Apple 提供的說明文件。

   透過 Sign in with Apple，以下是可能未傳回範圍的使用者案例：
   + 最終使用者在離開 Apple 的登入頁面之後遇到錯誤 (可能是 Amazon Cognito 的內部錯誤或開發人員撰寫的任何項目)
   + 服務 ID 識別碼在使用者集區和/或其他身分驗證服務之間使用
   + 開發人員在最終使用者先前登入之後新增其他範圍 (未擷取新的資訊)
   + 開發人員刪除使用者，然後使用者再次登入而未從他們的 Apple ID 描述檔移除應用程式

1. 將 IdP 的屬性對應至您的使用者集區。如需詳細資訊，請參閱[為您的使用者集區指定身分提供者屬性對應](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-specifying-attribute-mapping.html)。

1. 選擇**建立**。

1. 從**應用程式用戶端**功能表中，從清單中選擇應用程式用戶端。若要將新的社交身分提供者新增至應用程式用戶端，請導覽至**登入頁面**索引標籤，然後選取受管登入頁面上的**編輯**組態。 ****

1. 選擇**儲存變更**。

## 測試社交 IdP 組態
<a name="cognito-user-pools-social-idp-step-3"></a>

在您的應用程式中，您必須在使用者的用戶端中叫用瀏覽器，以便他們可以使用其社交供應商登入。在您完成上述章節中的設定程序後，向您的社交供應商測試登入。下列範例 URL 會使用字首網域載入使用者集區的登入頁面。

```
https://{{mydomain.auth.us-east-1.amazoncognito.com}}/oauth2/authorize?response_type=code&client_id={{1example23456789}}&redirect_uri={{https://www.example.com}}
```

此連結是當您前往**應用程式用戶端**功能表、選取應用程式用戶端、導覽至**登入頁面**索引標籤，然後選取**檢視登入頁面**時，Amazon Cognito 會引導您前往的頁面。如需使用者集區網域的詳細資訊，請參閱 [設定使用者集區網域](cognito-user-pools-assign-domain.md)。如需應用程式用戶端的詳細資訊，包括用戶端 IDs和回呼 URLs，請參閱 [使用應用程式用戶端的應用程式特定設定](user-pool-settings-client-apps.md)。

下列範例連結[授權端點](authorization-endpoint.md)會使用`identity_provider`查詢參數，從 設定無提示重新導向至社交供應商。此 URL 會略過具有受管登入的互動式使用者集區登入，並直接前往 IdP 登入頁面。

```
https://{{mydomain.auth.us-east-1.amazoncognito.com}}/oauth2/authorize?identity_provider={{Facebook|Google|LoginWithAmazon|SignInWithApple}}&response_type=code&client_id={{1example23456789}}&redirect_uri={{https://www.example.com}}
```