總覽

透過帳戶連結功能,Google 帳戶持有人就能快速、順暢又安全地連線至您的服務。您可以選擇導入 Google 帳戶連結功能,將平台中使用者資料提供給 Google 應用程式和服務。

安全的 OAuth 2.0 通訊協定可讓您將使用者的 Google 帳戶與平台上的使用者帳戶安全地連結,藉此授權 Google 應用程式和裝置存取您的服務。

使用者可以連結或取消連結帳戶,也能選擇在平台上使用 Google 帳戶連結功能建立新帳戶。

應用情境

導入 Google 帳戶連結功能的部分原因如下:

  • 與 Google 應用程式和服務共用平台上的使用者資料。

  • 使用 Google TV 播放影片和電影內容。

  • 使用 Google Home 應用程式和 Google 助理說出「Ok Google,開燈」,管理及控制已連線的 Google 智慧型住宅裝置。

  • 使用對話動作「Ok Google,用星巴克點餐」,打造使用者自訂的 Google 助理體驗和功能。

  • 只要將使用者的 Google 帳戶連結至獎勵合作夥伴帳戶,即可在 YouTube 上觀看符合資格的直播,藉此賺取獎勵。

  • 註冊時,使用 Google 帳戶設定檔的共用資料預先填入新帳戶。

支援的功能

Google 帳戶連結支援以下功能:

  • 使用 OAuth 連結隱含流程,即可輕鬆快速地分享資料。

  • 透過 OAuth 連結授權碼流程,以加強安全性。

  • 將現有使用者登入,或為您的平台註冊新的 Google 已驗證使用者,取得他們的同意並使用簡化連結安全地共用資料。

  • 利用 App Flip 減少操作阻礙。在信任的 Google 應用程式中,輕觸一下即可安全地開啟已驗證的 Android 或 iOS 應用程式,輕觸一下即可授予使用者同意及連結帳戶。

  • 定義自訂範圍並且僅分享必要資料,以加強使用者隱私,並清楚定義資料的使用方式,提升使用者的信任感。

  • 您可以透過取消連結帳戶,撤銷平台所代管資料與服務的存取權。導入選用的權杖撤銷端點可讓您與 Google 啟動的事件保持同步;跨帳戶防護 (RISC) 則可讓您在平台上發生任何取消連結事件時通知 Google。

帳戶連結流程

共有 3 個 Google 帳戶連結流程,這些流程都以 OAuth 為基礎,因此需要您管理或控制符合 OAuth 2.0 規定的授權和權杖交換端點。

在連結過程中,您會在獲得帳戶持有人同意連結帳戶和共用資料後,向 Google 核發存取權杖給個別 Google 帳戶。

OAuth 連結 (「網路 OAuth」)

這是基本 OAuth 流程,可將使用者帶往您的網站進行連結。系統會將使用者重新導向至你的網站,以便登入帳戶。使用者登入後,即表示您同意利用您的服務將個人資料提供給 Google。生效後,使用者的 Google 帳戶和您的服務就會建立連結。

OAuth 連結支援授權碼和隱式 OAuth 流程。您的服務必須為隱式流程託管符合 OAuth 2.0 規定的授權端點,且在使用授權碼流程時,必須同時公開授權和權杖交換端點。

圖 1. 透過 Web OAuth 在使用者的手機上連結帳戶

以 OAuth 為基礎的應用程式翻轉連結 (「應用程式翻轉」)

這是一種 OAuth 流程,可將使用者帶往您的應用程式進行連結。

以 OAuth 為基礎的應用程式翻轉連結會在使用者於已驗證的 Android 或 iOS 行動應用程式和 Google 平台之間移動時,引導您查看提議的資料存取變更,並授予使用者同意將平台上的帳戶與 Google 帳戶建立連結。如要啟用應用程式翻轉,您的服務必須使用授權碼流程支援 OAuth 連結OAuth 式 Google 登入連結

App Flip 同時支援 AndroidiOS

運作方式:

Google 應用程式會檢查使用者的裝置是否已安裝應用程式:

  • 如果找到該應用程式,就會「翻轉」到您的應用程式。應用程式必須徵得使用者同意,才能將帳戶連結至 Google,然後再「返回」至 Google 途徑。
  • 如果系統找不到應用程式,或是在應用程式快速連結連結過程中發生錯誤,系統會將使用者重新導向至簡化流程或網路 OAuth 流程。

圖 2:使用 App Flip 在使用者的手機上連結帳戶

OAuth 簡化連結 (「簡化」)

以 OAuth 為基礎的 Google 登入簡化連結加入了 OAuth 連結之外的 Google 登入機制,讓使用者不必離開 Google 介面,就能完成連結程序,因此能減少操作上的阻礙。以 OAuth 為基礎的簡化連結能夠結合 Google 登入與 OAuth 連結,在提供順暢的登入、建立帳戶和帳戶連結方面提供最佳使用者體驗。您的服務必須支援符合 OAuth 2.0 規定的授權和權杖交換端點。此外,您的權杖交換端點必須支援 JSON Web Token (JWT) 斷言,並實作 checkcreateget 意圖。

運作方式:

Google 會宣告使用者帳戶,並將以下資訊傳送給您:

  • 如果使用者在您的資料庫中有帳戶,使用者成功將自己的 Google 帳戶與您服務中的帳戶建立連結。
  • 如果您的資料庫中沒有使用者帳戶,使用者可以使用 Google 提供的資訊建立新的第三方帳戶:電子郵件、名稱和個人資料相片,或是選擇登入並連結至其他電子郵件地址 (使用者必須透過 Web OAuth 登入服務)。

圖 3 透過簡化連結在使用者手機上進行帳戶連結

您應該使用哪個流程?

建議您導入所有流程,確保使用者獲得最佳連結體驗。簡化與應用程式翻轉流程可減少連結過程中的阻礙,因為使用者只需執行幾個步驟就能完成連結程序。網路 OAuth 連結作業最簡單,您可先從其他連結流程中加入開始。

使用權杖

Google 帳戶連結功能採用 OAuth 2.0 業界標準。

取得帳戶持有人同意連結個人帳戶及分享資料後,您會針對個別 Google 帳戶核發存取權杖給 Google。

Token types

OAuth 2.0 uses strings called tokens to communicate between the user agent, the client application, and the OAuth 2.0 server.

Three types of OAuth 2.0 tokens can be used during account linking:

  • Authorization code. A short-lived token that can be exchanged for an access and a refresh token. For security purposes, Google calls your authorization endpoint to obtain a single use or very short-lived code.

  • Access token. A token that grants the bearer access to a resource. To limit exposure that could result from the loss of this token, it has a limited lifetime, usually expiring after an hour or so.

  • Refresh token. A long-lived token that can be exchanged for a new access token when an access token expires. When your service integrates with Google, this token is exclusively stored and used by Google. Google calls your token exchange endpoint to exchange refresh tokens for access tokens, which are in turn used to access user data.

Token handling

Race conditions in clustered environments and client-server exchanges can result in complex timing and error handling scenarios when working with tokens. For example:

  • You receive a request for a new access token, and you issue a new access token. Concurrently, you receive a request for access to your service's resource using the previous, unexpired access token.
  • Your refresh token reply is yet to be received (or is never received) by Google. Meanwhile, the previously valid refresh token is used in a request from Google.

Requests and replies can arrive in any order, or not at all due to asynchronous services running in a cluster, network behavior, or other means.

Immediate and fully consistent shared state both within, and between, your and Google's token handling systems cannot be guaranteed. Multiple valid, unexpired tokens can coexist within or across systems short period of time. To minimize negative user impact we recommend you do the following:

  • Accept unexpired access tokens, even after a newer token is issued.
  • Use alternatives to Refresh Token Rotation.
  • Support multiple, concurrently valid access and refresh tokens. For security, you should limit the number of tokens and token lifetime.
Maintenance and outage handling

During maintenance or unplanned outages Google might be unable to call your authorization or token exchange endpoints to obtain access and refresh tokens.

Your endpoints should respond with a 503 error code and empty body. In this case, Google retries failed token exchange requests for a limited time. Provided that Google is later able to obtain refresh and access tokens, failed requests are not visible to users.

Failing requests for an access token result in a visible error, if initiated by a user. Users will be required to retry linking failures if the implicit OAuth 2.0 flow is used.

Recommendations

There are many solutions to minimize maintenance impact. Some options to consider:

  • Maintain your existing service and route a limited number of requests to your newly updated service. Migrate all requests only after confirming expected functionality.

  • Reduce the number of token requests during the maintenance period:

    • Limit maintenance periods to less than the access token lifetime.

    • Temporarily increase the access token lifetime:

      1. Increase token lifetime to greater than maintenance period.
      2. Wait twice the duration of your access token lifetime, enabling users to exchange short lived tokens for longer duration tokens.
      3. Enter maintenance.
      4. Respond to token requests with a 503 error code and empty body.
      5. Exit maintenance.
      6. Decrease token lifetime back to normal.

向 Google 註冊

我們需要您設定 OAuth 2.0 設定的詳細資料並提供憑證,才能啟用帳戶連結功能。詳情請參閱「註冊」一節。