「OAuth 與 Google 登入」連結類型除了 OAuth 以外,還可以添加 Google 登入功能 帳戶連結。讓 Google 使用者能夠順暢使用語音連結 也可以為已註冊服務的使用者啟用帳戶連結 以非 Google 身分登入
這種連結類型開頭為 Google 登入,可讓您檢查 您的系統中已有 Google 個人資料。如果使用者資訊 在您的系統中找不到,將啟動標準 OAuth 流程。使用者也可以 選擇以 Google 個人資料建立新的帳戶。
如要使用 OAuth 和 Google 登入功能進行帳戶連結,請遵循下列一般做法 步驟:
- 請先徵得使用者同意,允許存取自己的 Google 個人資料。
- 使用個人資料中的資訊來辨別使用者。
- 如果在驗證系統中找不到對應的 Google 使用者,
將如何繼續流程,取決於您是否已設定 Actions 專案
以便允許使用者透過語音建立使用者帳戶
你的網站
- 如果允許透過語音建立帳戶,請驗證身分證件 產生憑證接著,您就能根據 儲存在 ID 權杖中的設定檔資訊
- 如果您不允許透過語音建立帳戶,系統會將使用者轉移至 瀏覽器,可在其中載入您的授權頁面並完成使用者作業 建立流程
支援透過語音建立帳戶
如果您允許透過語音建立使用者帳戶,Google 助理會詢問使用者是否要 他們想要採取下列行動:
- 使用他人的 Google 帳戶資訊在您的系統中建立新帳戶,或
- 使用其他帳戶登入驗證系統 現有的非 Google 帳戶。
如果您希望盡量減少透過語音建立帳戶,建議允許透過語音建立帳戶 帳戶建立流程也會更加順暢使用者只需要離開語音流程 如果他們想使用現有的非 Google 帳戶登入。
不允許透過語音建立帳戶
如果您不允許透過語音建立使用者帳戶,Google 助理會開啟 您提供給使用者驗證的網站。如果互動是發生 使用者如果裝置沒有螢幕,Google 助理會將使用者導向手機 ,繼續進行帳戶連結流程。
在下列情況下,建議您禁止建立:
您不想開放非 Google 帳戶的使用者建立新的 使用者帳戶,並希望他們 將他們現有的使用者帳戶連結至 驗證系統。舉例來說,假設你提供會員方案 則希望確保使用者不會失去 自己在網站上累積的點數 現有帳戶。
您需要完全掌控帳戶建立流程。例如,您可以 如果需要在期間內向使用者顯示服務條款,則禁止建立 建立帳戶。
實作 OAuth 和 Google 登入帳戶連結
帳戶採用業界標準的 OAuth 2.0 流程連結。 Actions on Google 支援隱式和授權碼流程。
In the implicit code flow, Google opens your authorization endpoint in the user's browser. After successful sign in, you return a long-lived access token to Google. This access token is now included in every request sent from the Assistant to your Action.
In the authorization code flow, you need two endpoints:
- The authorization endpoint, which is responsible for presenting the sign-in UI to your users that aren't already signed in and recording consent to the requested access in the form of a short-lived authorization code.
- The token exchange endpoint, which is responsible for two types of exchanges:
- Exchanges an authorization code for a long-lived refresh token and a short-lived access token. This exchange happens when the user goes through the account linking flow.
- Exchanges a long-lived refresh token for a short-lived access token. This exchange happens when Google needs a new access token because the one it had expired.
Although the implicit code flow is simpler to implement, Google recommends that access tokens issued using the implicit flow never expire, because using token expiration with the implicit flow forces the user to link their account again. If you need token expiration for security reasons, you should strongly consider using the auth code flow instead.
設定專案
設定專案以使用 OAuth 和 Google 登入帳戶 連結,請按照下列步驟操作:
- 開啟 Actions 主控台,然後選取您要使用的專案。
- 按一下「開發」分頁標籤,然後選擇「帳戶連結」。
- 啟用「帳戶連結」旁的切換鈕。
- 在「建立帳戶」部分中選取「是」。
在「連結類型」中選取「OAuth 與」Google 登入和隱含。
在「Client Information」中,執行下列操作:
- 指派值給「Actions to Google」核發的用戶端 ID,用來識別用戶端 ID 以及來自 Google 的要求
- 插入授權和權杖交換端點的網址。
按一下 [儲存]。
實作 OAuth 伺服器
To support the OAuth 2.0 implicit flow, your service makes an authorization endpoint available by HTTPS. This endpoint is responsible for authenticating and obtaining consent from users for data access. The authorization endpoint presents a sign-in UI to your users that aren't already signed in and records consent to the requested access.
When your Action needs to call one of your service's authorized APIs, Google uses this endpoint to get permission from your users to call these APIs on their behalf.
A typical OAuth 2.0 implicit flow session initiated by Google has the following flow:
- Google opens your authorization endpoint in the user's browser. The user signs in if not signed in already, and grants Google permission to access their data with your API if they haven't already granted permission.
- Your service creates an access token and returns it to Google by redirecting the user's browser back to Google with the access token attached to the request.
- Google calls your service's APIs, and attaches the access token with each request. Your service verifies that the access token grants Google authorization to access the API and then completes the API call.
Handle authorization requests
When your Action needs to perform account linking via an OAuth2 implicit flow, Google sends the user to your authorization endpoint with a request that includes the following parameters:
| Authorization endpoint parameters | |
|---|---|
client_id |
The client ID you assigned to Google. |
redirect_uri |
The URL to which you send the response to this request. |
state |
A bookkeeping value that is passed back to Google unchanged in the redirect URI. |
response_type |
The type of value to return in the response. For the OAuth 2.0 implicit
flow, the response type is always token. |
For example, if your authorization endpoint is available at https://myservice.example.com/auth,
a request might look like:
GET https://myservice.example.com/auth?client_id=GOOGLE_CLIENT_ID&redirect_uri=REDIRECT_URI&state=STATE_STRING&response_type=token
For your authorization endpoint to handle sign-in requests, do the following steps:
Verify the
client_idandredirect_urivalues to prevent granting access to unintended or misconfigured client apps:- Confirm that the
client_idmatches the client ID you assigned to Google. - Confirm that the URL specified by the
redirect_uriparameter has the following form: YOUR_PROJECT_ID is the ID found on the Project settings page of the Actions Console.https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID
- Confirm that the
Check if the user is signed in to your service. If the user isn't signed in, complete your service's sign-in or sign-up flow.
Generate an access token that Google will use to access your API. The access token can be any string value, but it must uniquely represent the user and the client the token is for and must not be guessable.
Send an HTTP response that redirects the user's browser to the URL specified by the
redirect_uriparameter. Include all of the following parameters in the URL fragment:access_token: the access token you just generatedtoken_type: the stringbearerstate: the unmodified state value from the original request The following is an example of the resulting URL:https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID#access_token=ACCESS_TOKEN&token_type=bearer&state=STATE_STRING
Google's OAuth 2.0 redirect handler will receive the access token and confirm
that the state value hasn't changed. After Google has obtained an
access token for your service, Google will attach the token to subsequent calls
to your Action as part of the AppRequest.
處理自動連結
當使用者同意您的動作存取其 Google 個人資料後,Google 傳送的要求包含已簽署的 Google 使用者身分識別資訊。 聲明包含使用者的 Google 帳戶 ID、姓名、 以及電子郵件地址為專案設定的憑證交換端點會處理
如果驗證系統中已有對應的 Google 帳戶,
權杖交換端點會傳回使用者權杖如果 Google 帳戶不是
與現有使用者相符,權杖交換端點會傳回 user_not_found 錯誤。
這項要求的格式如下:
POST /token HTTP/1.1 Host: oauth2.example.com Content-Type: application/x-www-form-urlencoded grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&intent=get&assertion=JWT&consent_code=CONSENT_CODE&scope=SCOPES
您的權杖交換端點必須能處理下列參數:
| 權杖端點參數 | |
|---|---|
grant_type |
要交換的權杖類型。對於這些要求,這個
參數值為 urn:ietf:params:oauth:grant-type:jwt-bearer。 |
intent |
這類要求的參數值為 `get`。 |
assertion |
JSON Web Token (JWT),提供已簽署 識別使用者的身分JWT 包含使用者的 Google 帳戶 ID、名稱和電子郵件地址。 |
consent_code |
選用:出現一次性代碼時,系統會發送一次性代碼, 使用者已同意您的動作存取指定範圍。 |
scope |
選用:您設定 Google 向使用者要求的範圍。 |
當權杖交換端點收到連結要求時,應執行 包括:
Validate and decode the JWT assertion
You can validate and decode the JWT assertion by using a JWT-decoding library for your language. Use Google's public keys (available in JWK or PEM format) to verify the token's signature.
When decoded, the JWT assertion looks like the following example:
{ "sub": 1234567890, // The unique ID of the user's Google Account "iss": "https://accounts.google.com", // The assertion's issuer "aud": "123-abc.apps.googleusercontent.com", // Your server's client ID "iat": 233366400, // Unix timestamp of the assertion's creation time "exp": 233370000, // Unix timestamp of the assertion's expiration time "name": "Jan Jansen", "given_name": "Jan", "family_name": "Jansen", "email": "jan@gmail.com", // If present, the user's email address "locale": "en_US" }
In addition to verifying the token's signature, verify that the assertion's issuer
(iss field) is https://accounts.google.com and that the audience (aud field)
is the client ID assigned to your Action.
檢查驗證系統是否已有 Google 帳戶
確認是否符合下列任一條件:
- 在斷言的
sub欄位中,Google 帳戶 ID 位於您的使用者資料庫中。 - 斷言中的電子郵件地址與您使用者資料庫中的使用者相符。
如果任一條件為 true,表示使用者已註冊,您可以核發 存取權杖
如果聲明中未提供 Google 帳戶 ID 或電子郵件地址
與您資料庫中的使用者相符,這表示對方尚未註冊。在這種情況下,您的
權杖交換端點應傳回 HTTP 401 錯誤,指定 error=user_not_found。
如以下範例所示:
HTTP/1.1 401 Unauthorized
Content-Type: application/json;charset=UTF-8
{
"error":"user_not_found",
}
user_not_found 錯誤的 401 錯誤回應時,
使用 intent 參數值來呼叫權杖交換端點
設為 create 並傳送包含使用者個人資訊的 ID 權杖
。
透過 Google 登入功能處理帳戶建立作業
當使用者需要在您的服務上建立帳戶時,Google 會
要求傳送至權杖交換端點
intent=create,如以下範例所示:
POST /token HTTP/1.1 Host: oauth2.example.com Content-Type: application/x-www-form-urlencoded response_type=token&grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&scope=SCOPES&intent=create&consent_code=CONSENT_CODE&assertion=JWT[&NEW_ACCOUNT_INFO]
assertion 參數包含專為您提供的 JSON Web Token (JWT) 提供的
經簽署的 Google 使用者身分識別資訊。JWT 包含
包含使用者的 Google 帳戶 ID、名稱和電子郵件地址,您可以使用
為您的服務建立新帳戶。
如要回應帳戶建立要求,您的權杖交換端點必須 包括:
Validate and decode the JWT assertion
You can validate and decode the JWT assertion by using a JWT-decoding library for your language. Use Google's public keys (available in JWK or PEM format) to verify the token's signature.
When decoded, the JWT assertion looks like the following example:
{ "sub": 1234567890, // The unique ID of the user's Google Account "iss": "https://accounts.google.com", // The assertion's issuer "aud": "123-abc.apps.googleusercontent.com", // Your server's client ID "iat": 233366400, // Unix timestamp of the assertion's creation time "exp": 233370000, // Unix timestamp of the assertion's expiration time "name": "Jan Jansen", "given_name": "Jan", "family_name": "Jansen", "email": "jan@gmail.com", // If present, the user's email address "locale": "en_US" }
In addition to verifying the token's signature, verify that the assertion's issuer
(iss field) is https://accounts.google.com and that the audience (aud field)
is the client ID assigned to your Action.
驗證使用者資訊並建立新帳戶
確認是否符合下列任一條件:
- 在斷言的
sub欄位中,Google 帳戶 ID 位於您的使用者資料庫中。 - 斷言中的電子郵件地址與您使用者資料庫中的使用者相符。
如果符合任一條件,請提示使用者連結現有帳戶
回應該請求,並傳回 HTTP 401 錯誤,
error=linking_error 和使用者的電子郵件地址為 login_hint,如
範例:
HTTP/1.1 401 Unauthorized
Content-Type: application/json;charset=UTF-8
{
"error":"linking_error",
"login_hint":"foo@bar.com"
}
如果兩個條件皆不成立,請使用這項資訊建立新的使用者帳戶 使用這組 API新帳戶通常不會設定密碼。是 建議您在其他平台中加入 Google 登入功能,方便使用者登入 透過 Google 顯示在應用程式各平台上此外,也可以 透過電子郵件,使用可啟動密碼復原流程的連結,讓使用者完成設定 一組密碼登入其他平台
建立完畢後,請核發存取權杖 ,並傳回 JSON 物件中 也就是您的 HTTPS 回應內文,如以下範例所示:
{ "token_type": "Bearer", "access_token": "ACCESS_TOKEN", "expires_in": SECONDS_TO_EXPIRATION }
啟動驗證流程
使用帳戶登入輔助意圖 以啟動驗證流程
const app = dialogflow({ // REPLACE THE PLACEHOLDER WITH THE CLIENT_ID OF YOUR ACTIONS PROJECT clientId: CLIENT_ID, }) // Intent that starts the account linking flow. app.intent('Start Signin', conv => { conv.ask(new SignIn('To get your account details')) })
private String clientId = "<your_client_id>"; @ForIntent("Start Signin") public ActionResponse text(ActionRequest request) { ResponseBuilder rb = getResponseBuilder(request); return rb.add(new SignIn().setContext("To get your account details")).build(); }
const app = actionssdk({ clientId: CLIENT_ID, }) app.intent('Start Signin', conv => { conv.ask(new SignIn('To get your account details')) })
private String clientId = "<your_client_id>"; @ForIntent("actions.intent.TEXT") public ActionResponse text(ActionRequest request) { ResponseBuilder rb = getResponseBuilder(request); return rb.add(new SignIn().setContext("To get your account details")).build(); }
處理資料存取要求
如果 Google 助理要求包含存取權杖, 請先檢查存取權杖是否有效且尚未過期,然後再從 使用者帳戶資料庫。