サービスで OAuth 2.0 インプリシット フローをサポートするには、HTTPS 経由で認可エンドポイントにアクセスできるようにする必要があります。このエンドポイントは、ユーザーの認証を行い、ユーザーからデータアクセスへの同意を取得します。認可エンドポイントは、ログインしていないユーザーにログイン用の UI を表示し、リクエストされたアクセスへの同意を記録します。
Google アプリケーションからサービスの API を呼び出す必要がある場合、Google はこのエンドポイントを使用して、API の呼び出し許可をユーザーから取得します。
Google アカウントのリンク: OAuth 暗黙的フロー
次のシーケンス図は、ユーザー、Google、サービスの各エンドポイント間のインタラクションの詳細を示しています。
役割と責任
次の表に、Google アカウントのリンク(GAL)OAuth 暗黙的フローにおけるアクターの役割と責任を示します。GAL では、Google は OAuth クライアントとして機能し、サービスはID/サービス プロバイダとして機能します。
| アクター / コンポーネント | GAL の役割 | 責任 |
|---|---|---|
| Google アプリ / サーバー | OAuth クライアント | フローを開始し、ブラウザのリダイレクトを使用してアクセス トークンを受け取り、サービス API にアクセスするために安全に保存します。 |
| 認可エンドポイント | 認可サーバー | ユーザーを認証し、ユーザーの同意を取得して、有効期間の長いアクセス トークンを Google に直接発行します。 |
| Google リダイレクト URI | コールバック エンドポイント | URL フラグメントに access_token 値と state 値を含む、承認サービスからのユーザー リダイレクトを受け取ります。 |
通常、Google が開始する OAuth 2.0 暗黙的フローのセッションは次のような流れになります。
- Google がユーザーのブラウザで認可エンドポイントを開きます。ユーザーがログインし(ログインしていない場合)、Google が API を使用してデータにアクセスすることを承諾します(まだ許可していない場合)。
- サービスがアクセス トークンを作成し、Google に返します。そのためには、アクセス トークンをリクエストに付加して、ユーザーのブラウザを Google にリダイレクトします。
- Google がサービスの API を呼び出し、リクエストごとにアクセス トークンを関連付けます。サービスは、アクセス トークンによって API へのアクセスが Google に許可されていることを確認し、API 呼び出しを完了します。
実装レシピ
暗黙的フローを実装する手順は次のとおりです。
ステップ 1: 認可リクエストを処理する
Google がアカウントのリンクを開始すると、ユーザーは認証エンドポイントにリダイレクトされます。プロトコル コントラクトとパラメータ要件の詳細については、認証エンドポイントをご覧ください。
リクエストを処理するには、次の操作を行います。
リクエストを検証します。
client_idが Google に割り当てられたクライアント ID と一致することを確認します。redirect_uriが想定される Google リダイレクト URL と一致していることを確認します。none https://oauth-redirect.googleusercontent.com/r/YOUR_PARTNER_ID https://oauth-redirect-sandbox.googleusercontent.com/r/YOUR_PARTNER_IDresponse_typeがtokenであることを確認します。
ユーザーを認証する:
- ユーザーがサービスにログインしているかどうかを確認します。
- ユーザーがログインしていない場合は、ログインまたは登録フローを完了するよう促します。
アクセス トークンを生成する:
- ユーザーとクライアントに関連付けられた、推測不可能な一意のアクセス トークンを作成します。
Google にリダイレクトする:
- ブラウザを
redirect_uriで指定された URL にリダイレクトします。 - 次のパラメータを URL フラグメント(ハッシュ)に追加します。
access_token: 生成したアクセス トークン。token_type:bearerでなければなりません。state: Google から受信した変更されていない状態値。
- ブラウザを
Handle userinfo requests
The userinfo endpoint is an OAuth 2.0 protected resource that return claims about the linked user. Implementing and hosting the userinfo endpoint is optional, except for the following use cases:
- Linked Account Sign-In with Google One Tap.
- Frictionless subscription on AndroidTV.
After the access token has been successfully retrieved from your token endpoint, Google sends a request to your userinfo endpoint to retrieve basic profile information about the linked user.
| userinfo endpoint request headers | |
|---|---|
Authorization header |
The access token of type Bearer. |
For example, if your userinfo endpoint is available at
https://myservice.example.com/userinfo, a request might look like the following:
GET /userinfo HTTP/1.1 Host: myservice.example.com Authorization: Bearer ACCESS_TOKEN
For your userinfo endpoint to handle requests, do the following steps:
- Extract access token from the Authorization header and return information for the user associated with the access token.
- If the access token is invalid, return an HTTP 401 Unauthorized error with using the
WWW-AuthenticateResponse Header. Below is an example of a userinfo error response: If a 401 Unauthorized, or any other unsuccessful error response is returned during the linking process, the error will be non-recoverable, the retrieved token will be discarded and the user will have to initiate the linking process again.HTTP/1.1 401 Unauthorized WWW-Authenticate: error="invalid_token", error_description="The Access Token expired"
If the access token is valid, return and HTTP 200 response with the following JSON object in the body of the HTTPS response:
If your userinfo endpoint returns an HTTP 200 success response, the retrieved token and claims are registered against the user's Google account.{ "sub": "USER_UUID", "email": "EMAIL_ADDRESS", "given_name": "FIRST_NAME", "family_name": "LAST_NAME", "name": "FULL_NAME", "picture": "PROFILE_PICTURE", }userinfo endpoint response subA unique ID that identifies the user in your system. emailEmail address of the user. given_nameOptional: First name of the user. family_nameOptional: Last name of the user. nameOptional: Full name of the user. pictureOptional: Profile picture of the user.