OAuth ベースの Google ログインの「効率的なリンク」タイプは、OAuth ベースのアカウント リンクの上に Google ログインを追加します。これにより、Google ユーザーには音声ベースのシームレスなリンクが提供されます。また、Google 以外の ID でサービスに登録したユーザーに対しては、アカウントのリンクが有効になります。
このリンクタイプは Google ログインから始まります。Google ログインを使用すると、ユーザーの Google プロフィール情報がシステムに存在するかどうかを確認できます。ユーザーの情報がシステムで見つからなかった場合は、標準の OAuth フローが開始されます。ユーザーは、Google プロフィール情報を使用して新しいアカウントを作成することを選択することもできます。

「合理化されたリンクタイプ」を使用してアカウント リンクを実行する一般的な手順は次のとおりです。
- まず、ユーザーの Google プロフィールにアクセスすることについてユーザーに同意を求めます。
- プロフィールの情報を使用してユーザーを識別します。
- 認証システムで Google ユーザーに一致するユーザーが見つからない場合は、Actions Console で Actions プロジェクトを構成し、音声によるユーザー アカウントの作成を許可する構成か、ウェブサイトでのユーザー アカウントの作成のみを許可する構成であるかによってフローが進められます。
- 音声によるアカウントの作成を許可する場合は、Google から受け取った ID トークンを検証します。その後、ID トークンに含まれるプロフィール情報に基づいてユーザーを作成できます。
- 音声によるアカウントの作成を許可しない場合、ユーザーはブラウザに転送され、そこで認証ページを読み込み、ユーザー作成フローを完了できます。

音声によるアカウント作成を許可する
音声によるユーザー アカウントの作成を許可すると、アシスタントはユーザーに次のことを行うかどうかを確認します。
- Google アカウント情報を使用してシステムに新しいアカウントを作成する。
- Google 以外のアカウントがすでにある場合は、別のアカウントで認証システムにログインする。
アカウント作成フローをスムーズに進めたい場合は、音声によるアカウント作成を許可することをおすすめします。ユーザーは Google 以外の既存のアカウントを使用してログインする場合のみ、音声フローから離れます。
音声によるアカウント作成を許可しない
音声によるユーザー アカウントの作成を許可しない場合、アシスタントはユーザー認証用に指定したウェブサイトの URL を開きます。画面のないデバイスで操作が行われている場合、アシスタントはユーザーをスマートフォンに誘導し、アカウント リンクのフローを続行します。
次の場合はアカウント作成を許可しないことをおすすめします。
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.
プロジェクトを構成する
合理化されたリンクを使用するようにプロジェクトを設定する手順は次のとおりです。
- Actions Console を開き、使用するプロジェクトを選択します。
- [Develop](開発)タブをクリックして、[Account linking](アカウント リンク)を選択します。
- [アカウントのリンク] の横にあるスイッチを有効にします。
- [Account creation] で、[Yes] を選択します。
[Linking type] で、[OAuth & Google Sign In](OAuth および Google ログイン)と [Implicit](インプリシット)を選択します。
[Client Information] で、次の操作を行います。
- [Actions to Google に発行するクライアント ID] に値を割り当てます。これは、Google からのリクエストを識別する値です。
- 認可エンドポイントとトークン交換エンドポイントの URL を挿入します。
[保存] をクリックします。
OAuth サーバーを実装する
OAuth 2.0 暗黙的フローをサポートするために、サービスは 使用できます。このエンドポイントは、認証と認可を担当します。 データアクセスについてユーザーから同意を得る。認可エンドポイントは、ログインしていないユーザーにログイン用の UI を表示し、リクエストされたアクセスへの同意を記録します。
アクションからサービスの API を呼び出す必要がある場合、Google はこのエンドポイントを使用して、API の呼び出し許可をユーザーから取得します。
通常、Google が開始する OAuth 2.0 インプリシット フローのセッションは次のような流れになります。
- Google がユーザーのブラウザで認可エンドポイントを開きます。ユーザーがログインし(ログインしていない場合)、Google が API を使用してデータにアクセスすることを承諾します(まだ許可していない場合)。
- サービスがアクセス トークンを作成します。そのトークンを含むリクエストを送信して、ユーザーのブラウザを Google にリダイレクトし、Google にアクセス トークンを返します。
- Google がサービスの API を呼び出し、リクエストごとにアクセス トークンを関連付けます。サービスは、アクセス トークンによって API へのアクセスが Google に許可されていることを確認し、API 呼び出しを完了します。
認可リクエストの処理
アクションが OAuth 2.0 暗黙的フローを介してアカウント リンクを実行する必要がある場合、 Google は、次を含むリクエストでユーザーを認可エンドポイントに送信します。 次のパラメータを指定します。
認可エンドポイントのパラメータ | |
---|---|
client_id |
Google に割り当てたクライアント ID。 |
redirect_uri |
このリクエストに対するレスポンスを送信する URL。 |
state |
リダイレクト URL で変更されずに Google に返される会計上の値。 |
response_type |
レスポンスで返される値のタイプ。OAuth 2.0 暗黙の API では、
レスポンス タイプは常に token です。 |
たとえば、認可エンドポイントが https://myservice.example.com/auth
にある場合、
次のようなリクエストになります。
GET https://myservice.example.com/auth?client_id=GOOGLE_CLIENT_ID &redirect_uri=REDIRECT_URI &state=STATE_STRING &response_type=token
認可エンドポイントでログイン リクエストを処理する場合は、次の手順に従います。
client_id
とredirect_uri
の値を検証する 意図しないクライアント アプリへのアクセスや構成ミスのあるクライアント アプリclient_id
が、指定したクライアント ID と一致することを確認します。 割り当てられていますredirect_uri
で指定された URL を確認します。 パラメータの形式は次のとおりです。 YOUR_PROJECT_ID は、[プロジェクトの設定] ページにある ID です。 確認できます。https://oauth-redirect.googleusercontent.com/r/
YOUR_PROJECT_ID
ユーザーがサービスにログインしているかどうか確認します。ユーザーがログインしていない場合は、サービスのログインまたは登録フローを完了します。
Google が API へのアクセスで使用するアクセス トークンを生成します。アクセス トークンには任意の文字列値を設定できますが、トークンを使用するユーザーとクライアントを一意に表し、簡単に推測されない文字列にする必要があります。
ユーザーのブラウザを URL にリダイレクトする HTTP レスポンスを送信する
redirect_uri
パラメータで指定します。URL フラグメントに次のパラメータをすべて含めます。access_token
: 生成したアクセス トークンtoken_type
: 文字列bearer
state
: 元の状態から変更されていない状態の値 リクエスト 結果の URL の例を次に示します。https://oauth-redirect.googleusercontent.com/r/
YOUR_PROJECT_ID #access_token=ACCESS_TOKEN &token_type=bearer&state=STATE_STRING
Google の OAuth 2.0 リダイレクト ハンドラがアクセス トークンを受け取り、
state
値が変更されていないことを確認します。Google は、
そのトークンを後続の呼び出しにアタッチします。
AppRequest の一部としてアクションに渡します。
Handle automatic linking
After the user gives your Action consent to access their Google profile, Google sends a request that contains a signed assertion of the Google user's identity. The assertion contains information that includes the user's Google Account ID, name, and email address. The token exchange endpoint configured for your project handles that request.
If the corresponding Google account is already present in your authentication system,
your token exchange endpoint returns a token for the user. If the Google account doesn't
match an existing user, your token exchange endpoint returns a user_not_found
error.
The request has the following form:
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
Your token exchange endpoint must be able to handle the following parameters:
Token endpoint parameters | |
---|---|
grant_type |
The type of token being exchanged. For these requests, this
parameter has the value urn:ietf:params:oauth:grant-type:jwt-bearer . |
intent |
For these requests, the value of this parameter is `get`. |
assertion |
A JSON Web Token (JWT) that provides a signed assertion of the Google user's identity. The JWT contains information that includes the user's Google Account ID, name, and email address. |
consent_code |
Optional: When present, a one-time code that indicates that the user has granted consent for your Action to access the specified scopes. |
scope |
Optional: Any scopes you configured Google to request from users. |
When your token exchange endpoint receives the linking request, it should do the following:
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.
Check if the Google account is already present in your authentication system
Check whether either of the following conditions are true:
- The Google Account ID, found in the assertion's
sub
field, is in your user database. - The email address in the assertion matches a user in your user database.
If either condition is true, the user has already signed up and you can issue an access token.
If neither the Google Account ID nor the email address specified in the assertion
matches a user in your database, the user hasn't signed up yet. In this case, your
token exchange endpoint should reply with a HTTP 401 error, that specifies error=user_not_found
,
as in the following example:
HTTP/1.1 401 Unauthorized Content-Type: application/json;charset=UTF-8 { "error":"user_not_found", }
user_not_found
error, Google
calls your token exchange endpoint with the value of the intent
parameter
set to create and sending an ID token that contains the user's profile information
with the request.
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 ユーザーの ID に関する署名付きのアサーションです。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.
ユーザー情報を検証して新しいアカウントを作成する
次のいずれかの条件を満たしていることを確認します。
- Google アカウント ID(アサーションの
sub
フィールドにあります)は、ユーザー データベースにあります。 - アサーションに含まれているメールアドレスが、ユーザーのデータベースに登録されているユーザーと一致している。
いずれかの条件が true の場合、ユーザーに既存のアカウントをリンクするよう促す
そのリクエストに対して HTTP 401 エラーを返し、Google アカウント
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" }
どちらの条件も true でない場合は、その情報を使用して新しいユーザー アカウントを作成します。 JWT で提供します。通常、新しいアカウントにはパスワードが設定されていません。ユーザーがアプリケーションから Google にログインできるように、他のプラットフォームに Google ログインを追加することをおすすめします。また、他のプラットフォームでログイン パスワードを設定できるように、パスワード再設定フローを開始するリンクをメールで送信することもできます。
作成が完了したら、アクセス トークンを発行します。 JSON オブジェクトで値を HTTPS レスポンスの本文に URL を付加します。
{
"token_type": "Bearer",
"access_token": "ACCESS_TOKEN ",
"expires_in":SECONDS_TO_EXPIRATION
}
認証フローの音声ユーザー インターフェースを設計する
本人確認が完了しているかどうか確認してアカウントのリンクのフローを開始する
- Actions Console で Actions Builder プロジェクトを開きます。
- 新しいシーンを作成し、アクションでアカウントのリンクを開始します。
- [Scenes](シーン)をクリックします。
- 追加(+)アイコンをクリックして新しいシーンを追加します。
- 新しく作成したシーンで、[Conditions] の追加アイコン add アイコンをクリックします。
- 会話に関連付けられたユーザーが確認済みユーザーかどうかを確認する条件を追加します。チェックが失敗した場合、アクションは会話中にアカウント リンクを実行できないため、アカウント リンクを必要としない機能にアクセスできるようにフォールバックする必要があります。
- [Condition] の
Enter new expression
フィールドに、次のロジックを入力します。user.verificationStatus != "VERIFIED"
- [Transition] で、アカウントのリンクが不要なシーン、またはゲスト専用機能のエントリ ポイントとなるシーンを選択します。
- [Condition] の
- [Conditions] の追加アイコン add をクリックします。
- ユーザーに ID が関連付けられていない場合に、アカウントのリンクのフローをトリガーする条件を追加します。
- [Condition] の
Enter new expression
フィールドに、次のロジックを入力します。user.verificationStatus == "VERIFIED"
- [移行] で、[アカウントのリンク] システムシーンを選択します。
- [保存] をクリックします。
- [Condition] の
保存後、<SceneName>_AccountLinking
という新しいアカウント リンク システムシーンがプロジェクトに追加されます。
アカウントのリンクのシーンをカスタマイズする
- [Scenes](シーン)で、アカウント リンクのシステムシーンを選択します。
- [プロンプトを送信] をクリックし、アクションが ID にアクセスする必要がある理由をユーザーに説明する短い文を追加します(「設定を保存するため」など)。
- [保存] をクリックします。
- [条件] で [ユーザーがアカウントのリンクを正常に完了した場合] をクリックします。
- ユーザーがアカウントのリンクに同意した場合のフローの動作を設定します。 たとえば、Webhook を呼び出して必要なカスタム ビジネス ロジックを処理し、元のシーンに戻ります。
- [保存] をクリックします。
- [条件] で、[ユーザーがアカウントのリンクをキャンセルまたは拒否した場合] をクリックします。
- ユーザーがアカウントのリンクに同意しなかった場合にフローがどのように進むかを構成します。たとえば、確認応答メッセージを送信し、アカウント リンクを必要としない機能を提供するシーンにリダイレクトします。
- [保存] をクリックします。
- [条件] で [システムエラーまたはネットワーク エラーが発生した場合] をクリックします。
- システムエラーまたはネットワーク エラーが原因でアカウント リンクのフローを完了できない場合のフローの構成。たとえば、確認応答メッセージを送信し、アカウント リンクを必要としない機能を提供するシーンにリダイレクトします。
- [保存] をクリックします。
データアクセス リクエストを処理する
アシスタントのリクエストにアクセス トークンが含まれている場合は、まずそのアクセス トークンが有効で期限切れになっていないことを確認してから、そのトークンに関連付けられているユーザー アカウントをユーザー アカウント データベースから取得します。