すべての Cloud-to-cloud 統合には、ユーザーを認証するための仕組みを組み込む必要があります。
認証を使用すると、ユーザーの Google アカウントと認証システム内のユーザー アカウントを関連付けることができます。これにより、フルフィルメントでスマートホーム インテントを受け取ったときにユーザーを識別できます。Google スマートホームは、認証コードフローを使用した OAuth のみに対応します。
このページでは、OAuth 2.0 サーバーを設定して Cloud-to-cloud 統合と連携させる方法について説明します。
OAuth による Google アカウントのリンク
In the authorization code flow, you need two endpoints:
The authorization endpoint, which presents the sign-in UI to your users that aren't already signed in. The authorization endpoint also creates a short-lived authorization code to record users' consent to the requested access.
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.
Design guidelines
This section describes the design requirements and recommendations for the user screen that you host for OAuth linking flows. After it's called by Google's app, your platform displays a sign in to Google page and account linking consent screen to the user. The user is directed back to Google's app after giving their consent to link accounts.

Requirements
- You must communicate that the user’s account will be linked to Google, not a specific Google product like Google Home or Google Assistant.
- You must have a Google authorization statement such as "By signing in, you are authorizing Google to control your devices." See the Google Device Control Authorization section of the Google Home Developer Policies.
- You must provide a way for users to go back or cancel, if they choose not to link.
- You must open the Web OAuth linking page and ensure users have a clear method for signing in to their Google Account, such as fields for their username and password. Don't use the Google Sign-In (GSI) method that enables users to link without being taken to the Web OAuth Linking page. It is a violation of Google policy.
Recommendations
We recommend that you do the following:
Display Google's Privacy Policy. Include a link to Google’s Privacy Policy on the consent screen.
Data to be shared. Use clear and concise language to tell the user what data of theirs Google requires and why.
Clear call-to-action. State a clear call-to-action on your consent screen, such as “Agree and link.” This is because users need to understand what data they're required to share with Google to link their accounts.
Ability to unlink. Offer a mechanism for users to unlink, such as a URL to their account settings on your platform. Alternatively, you can include a link to Google Account where users can manage their linked account.
Ability to change user account. Suggest a method for users to switch their account(s). This is especially beneficial if users tend to have multiple accounts.
- If a user must close the consent screen to switch accounts, send a recoverable error to Google so the user can sign in to the desired account with OAuth linking.
Include your logo. Display your company logo on the consent screen. Use your style guidelines to place your logo. If you wish to also display Google's logo, see Logos and trademarks.

認証コードのフロー
認可コードフローの OAuth 2.0 サーバーは 2 つのエンドポイントで構成されます。これらのエンドポイントには HTTPS 経由でアクセスできます。最初のエンドポイントは認可エンドポイントで、これはデータアクセスに対するユーザーの同意を検索または取得する処理を担当します。認可エンドポイントは、ログインしていないユーザーにログイン用の UI を表示し、リクエストされたアクセスへの同意を記録します。2 つ目のエンドポイントはトークン交換エンドポイントで、トークンという暗号化された文字列を取得し、ユーザーにサービスへのアクセスを許可します。
Google アプリケーションでサービスの API を呼び出す必要がある場合、Google はこれらのエンドポイントを使用して、API の呼び出し許可をユーザーから取得します。
Google が開始する OAuth 2.0 認可コードフローのセッションは次のような流れになります。
- Google がユーザーのブラウザで認可エンドポイントを開きます。アクションのフローが音声専用デバイスで開始された場合、Google は実行をスマートフォンに転送します。
- ユーザーがログインし(ログインしていない場合)、Google が API を使用してデータにアクセスすることを承諾します(まだ許可していない場合)。
- サービスが認可コードを作成して Google に返します。これを行うには、認可コードをリクエストに付加してユーザーのブラウザを Google にリダイレクトします。
- Google が認可コードをトークン交換エンドポイントに送信します。トークン交換エンドポイントはコードの真正性を検証し、アクセス トークンと更新トークンを返します。アクセス トークンは、API にアクセスするための認証情報としてサービスが受け入れる短期のトークンです。更新トークンは長期のトークンで、アクセス トークンが期限切れになったときに新しいトークンを取得するために使用されます。
- ユーザーがアカウント リンクフローを完了すると、Google から送信されるすべてのリクエストにアクセス トークンが含まれます。
認可リクエストの処理
OAuth 2.0 認可コードフローを介してアカウント リンクを行う必要がある場合、Google は、次のパラメータを含むリクエストを使用して、ユーザーを認可エンドポイントに送信します。
認可エンドポイントのパラメータ | |
---|---|
client_id |
Google に割り当てたクライアント ID。 |
redirect_uri |
このリクエストに対するレスポンスを送信する宛先 URL。 |
state |
リダイレクト URL で変更されずに Google に返される会計上の値。 |
scope |
省略可: Google が許可を求めているデータ範囲を表す、スペースで区切られた文字列。 |
response_type |
レスポンスで返される値のタイプ。OAuth 2.0 認可コードフローの場合、レスポンス タイプは常に code です。 |
user_locale |
Google アカウントの言語設定(RFC5646 形式)。ユーザーの優先言語でコンテンツをローカライズするために使用されます。 |
たとえば、認可エンドポイントが https://myservice.example.com/auth
にある場合、リクエストは次のようになります。
GET https://myservice.example.com/auth?client_id=GOOGLE_CLIENT_ID&redirect_uri=REDIRECT_URI&state=STATE_STRING&scope=REQUESTED_SCOPES&response_type=code&user_locale=LOCALE
認可エンドポイントでログイン リクエストを処理する場合は、次の手順に従います。
client_id
が Google に割り当てたクライアント ID と一致し、redirect_uri
が Google からサービスに提供されたリダイレクト URL と一致することを確認します。これらのチェックは、意図しないクライアント アプリや誤って構成されたクライアント アプリによるアクセスを防ぐために重要です。複数の OAuth 2.0 フローをサポートしている場合は、response_type
がcode
であることも確認してください。- ユーザーがサービスにログインしているかどうか確認します。ユーザーがログインしていない場合は、サービスのログインまたは登録フローを完了します。
- Google が API へのアクセスに使用する認可コードを生成します。認可コードには任意の文字列値を設定できますが、トークンを使用するユーザーとクライアント、コードの有効期限を一意に表す必要があります。また、簡単に推測されない文字列にする必要があります。通常、約 10 分後に期限切れになる認可コードを発行します。
redirect_uri
パラメータで指定された URL が次の形式になっていることを確認します。https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID https://oauth-redirect-sandbox.googleusercontent.com/r/YOUR_PROJECT_ID
redirect_uri
パラメータで指定された URL にユーザーのブラウザをリダイレクトします。code
パラメータとstate
パラメータを追加してリダイレクトする場合は、生成された認可コードと元の未変更の state 値を含めます。結果の URL の例を次に示します。https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID?code=AUTHORIZATION_CODE&state=STATE_STRING
トークン交換リクエストの処理
サービスのトークン交換エンドポイントは、次の 2 種類のトークン交換を処理します。
- 認可コードとアクセス トークンおよび更新トークンとの交換
- 更新トークンとアクセス トークンの交換
トークン交換リクエストには、次のパラメータを使用します。
トークン交換エンドポイントのパラメータ | |
---|---|
client_id |
リクエスト元を Google として識別する文字列。この文字列は、Google の一意の識別子としてシステムに登録されている必要があります。 |
client_secret |
Google に登録したサービスのシークレット文字列。 |
grant_type |
交換されるトークンの種類。authorization_code または refresh_token のいずれかです。 |
code |
grant_type=authorization_code の場合、このパラメータは、Google がログインまたはトークン交換エンドポイントから受け取ったコードです。 |
redirect_uri |
grant_type=authorization_code の場合、このパラメータは最初の認可リクエストで使用される URL です。 |
refresh_token |
grant_type=refresh_token の場合、このパラメータは Google がトークン交換エンドポイントから受け取った更新トークンです。 |
Google がサーバーに認証情報を送信する方法を設定する
実装に応じて、認可サーバーはリクエスト本文またはリクエスト ヘッダーでクライアント認証情報を受信することを想定しています。
デフォルトでは、認証情報はリクエスト本文で送信されます。認可サーバーでリクエスト ヘッダーにクライアント認証情報を含める必要がある場合は、それに応じて Cloud-to-cloud 統合を構成する必要があります。
プロジェクトのリストで、操作するプロジェクトの横にある [開く] をクリックします。
[Cloud-to-Cloud] で [Develop] を選択します。
統合の横にある [開く] をクリックします。
[権限(省略可)] までスクロールし、[Google が HTTP 基本認証ヘッダーを介してクライアント ID とシークレットを送信する] チェックボックスをオンにします。
[保存] をクリックして、変更を保存します。
認可コードとアクセス トークンおよび更新トークンとの交換
ユーザーがログインして認可エンドポイントが短期の認可コードを Google に返すと、Google がリクエストをトークン交換エンドポイントに送信し、アクセス トークンと更新トークンの認可コードを交換します。
このリクエストの場合、grant_type
の値は authorization_code
で、code
の値は以前に Google に付与した認可コードの値になります。認可コードをアクセス トークンと更新トークンと交換するリクエストは次のようになります。
POST /token HTTP/1.1 Host: oauth2.example.com Content-Type: application/x-www-form-urlencoded client_id=GOOGLE_CLIENT_ID&client_secret=GOOGLE_CLIENT_SECRET&grant_type=authorization_code&code=AUTHORIZATION_CODE&redirect_uri=REDIRECT_URI
認可コードをアクセス トークンと更新トークンと交換するため、トークン交換エンドポイントは次の処理を行い、POST
リクエストに応答します。
client_id
がリクエスト元を認証済みオリジンとして識別し、client_secret
が期待値と一致していることを確認します。- 認可コードが有効で、期限が切れていないことを確認します。さらに、リクエストで指定されたクライアント ID が、認可コードに関連付けられたクライアント ID と一致することを確認します。
redirect_uri
パラメータで指定された URL が、最初の認可リクエストで使用された値と一致していることを確認します。- 上記の条件のすべて満たしていない場合、本文で HTTP 400 Bad Request エラーと
{"error": "invalid_grant"}
を返します。 - それ以外の場合は、認可コードのユーザー ID を使用して、更新トークンとアクセス トークンを生成します。これらのトークンには任意の文字列値を設定できますが、トークンを使用するユーザーとクライアントを一意に表し、簡単に推測されない文字列にする必要があります。アクセス トークンには、トークンの有効期限(通常はトークンを発行してから 1 時間)も記録します。更新トークンに有効期限はありません。
- HTTPS レスポンスの本文で次の JSON オブジェクトを返します。
{ "token_type": "Bearer", "access_token": "ACCESS_TOKEN", "refresh_token": "REFRESH_TOKEN", "expires_in": SECONDS_TO_EXPIRATION }
Google は、ユーザーのアクセス トークンと更新トークンを保存し、アクセス トークンの有効期限を記録します。アクセス トークンの有効期限が切れると、Google は更新トークンを使用してトークン交換エンドポイントから新しいアクセス トークンを取得します。
更新トークンとアクセス トークンの交換
アクセス トークンが期限切れになると、Google は、更新トークンを新しいアクセス トークンと交換するためのリクエストをトークン交換エンドポイントに送信します。
これらのリクエストの場合、grant_type
の値は refresh_token
で、refresh_token
の値は以前に Google に付与した更新トークンの値です。更新トークンとアクセス トークンの交換リクエストの例を次に示します。
POST /token HTTP/1.1 Host: oauth2.example.com Content-Type: application/x-www-form-urlencoded client_id=GOOGLE_CLIENT_ID&client_secret=GOOGLE_CLIENT_SECRET&grant_type=refresh_token&refresh_token=REFRESH_TOKEN
更新トークンとアクセス トークンを交換するため、トークン交換エンドポイントが次の処理を行い、POST
リクエストに応答します。
client_id
がリクエスト元を Google として識別し、client_secret
が期待値と一致していることを確認します。- 更新トークンが有効で、リクエストで指定されたクライアント ID が更新に関連付けられたクライアント ID と一致していることを確認します。
- 上記の条件のすべて満たしていない場合、本文で HTTP 400 Bad Request エラーと
{"error": "invalid_grant"}
を返します。 - それ以外の場合は、更新トークンのユーザー ID を使用してアクセス トークンを生成します。これらのトークンには任意の文字列値を設定できますが、トークンを使用するユーザーとクライアントを一意に表し、簡単に推測されない文字列にする必要があります。アクセス トークンには、トークンの有効期限(通常はトークンを発行してから 1 時間)も記録します。
- HTTPS レスポンスの本文で次の JSON オブジェクトを返します。
{ "token_type": "Bearer", "access_token": "ACCESS_TOKEN", "expires_in": SECONDS_TO_EXPIRATION }
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-Authenticate
Response 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 sub
A unique ID that identifies the user in your system. email
Email address of the user. given_name
Optional: First name of the user. family_name
Optional: Last name of the user. name
Optional: Full name of the user. picture
Optional: Profile picture of the user.