계정은 업계 표준 OAuth 2.0 암시적 흐름 및 승인 코드 흐름을 사용하여 연결됩니다. 서비스가 OAuth 2.0을 준수하는 승인 및 토큰 교환 엔드포인트를 지원해야 합니다.
In the implicit 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 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.
Choose an OAuth 2.0 flow
Although the implicit flow is simpler to implement, Google recommends that access tokens issued by the implicit flow never expire. This is because the user is forced to link their account again after a token expires with the implicit flow. If you need token expiration for security reasons, we strongly recommend that you use the authorization code flow instead.
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.
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 cancel. Provide a way for users to go back or cancel, if they choose not to link.
Clear sign-in process. Ensure that users have clear method for signing in to their Google account, such as fields for their username and password or Sign in with Google.
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 and the implicit flow.
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.
Create the project
To create your project to use account linking:
- Go to the Google API Console.
- Click Create project.
- Enter a name or accept the generated suggestion.
- Confirm or edit any remaining fields.
- Click Create.
To view your project ID:
- Go to the Google API Console.
- Find your project in the table on the landing page. The project ID appears in the ID column.
Configure your OAuth Consent Screen
The Google Account Linking process includes a consent screen which tells users the application requesting access to their data, what kind of data they are asking for and the terms that apply. You will need to configure your OAuth consent screen before generating a Google API client ID.
- Open the OAuth consent screen page of the Google APIs console.
- If prompted, select the project you just created.
On the "OAuth consent screen" page, fill out the form and click the “Save” button.
Application name: The name of the application asking for consent. The name should accurately reflect your application and be consistent with the application name users see elsewhere. The application name will be shown on the Account Linking consent screen.
Application logo: An image on the consent screen that will help users recognize your app. The logo is shown on Account linking consent screen and on account settings
Support email: For users to contact you with questions about their consent.
Scopes for Google APIs: Scopes allow your application to access your user's private Google data. For the Google Account Linking use case, default scope (email, profile, openid) is sufficient, you don’t need to add any sensitive scopes. It is generally a best practice to request scopes incrementally, at the time access is required, rather than up front. Learn more.
Authorized domains: To protect you and your users, Google only allows applications that authenticate using OAuth to use Authorized Domains. Your applications' links must be hosted on Authorized Domains. Learn more.
Application Homepage link: Home page for your application. Must be hosted on an Authorized Domain.
Application Privacy Policy link: Shown on Google Account Linking consent screen. Must be hosted on an Authorized Domain.
Application Terms of Service link (Optional): Must be hosted on an Authorized Domain.
Figure 1. Google Account Linking Consent Screen for a fictitious Application, Tunery
Check "Verification Status", if your application needs verification then click the "Submit For Verification" button to submit your application for verification. Refer to OAuth verification requirements for details.
OAuth 서버 구현
OAuth 2.0 암시적 흐름을 지원하기 위해 서비스 승인 사용할 수 있습니다 이 엔드포인트는 인증과 데이터 액세스에 대한 사용자의 동의 받기 승인 엔드포인트 아직 로그인하지 않은 사용자에게 로그인 UI를 표시하고 액세스할 수 있도록 합니다.
Google 애플리케이션이 서비스의 승인된 API 중 하나를 호출해야 하는 경우 Google은 이 엔드포인트를 사용하여 사용자로부터 이러한 API를 호출할 권한을 얻습니다. 위임할 수 있습니다
Google에서 시작한 일반적인 OAuth 2.0 암시적 흐름 세션에는 다음 흐름을 따라 하세요.
- Google은 사용자의 브라우저에서 승인 엔드포인트를 엽니다. 이 사용자가 아직 로그인하지 않은 경우 로그인하고 Google에 다음 권한을 부여합니다. 아직 권한을 부여하지 않은 경우 API를 통해 데이터에 액세스할 수 있습니다.
- 서비스에서 액세스 토큰을 만들고 이를 Google 이렇게 하려면 사용자의 브라우저를 액세스 권한이 있는 Google로 다시 리디렉션합니다. 토큰입니다.
- Google은 서비스의 API를 호출하고 실행할 수 있습니다 서비스에서 액세스 토큰이 Google에 인증한 다음 API 호출을 완료합니다.
승인 요청 처리
Google 애플리케이션에서 OAuth 2.0을 통해 계정 연결을 수행해야 하는 경우 Google은 인증 엔드포인트로 사용자를 다음과 같은 매개변수를 포함합니다.
| 승인 엔드포인트 매개변수 | |
|---|---|
client_id |
Google에 할당한 클라이언트 ID입니다. |
redirect_uri |
이 요청에 대한 응답을 보낼 URL입니다. |
state |
정해진 기간에 변경되지 않고 Google에 다시 전달되는 리디렉션 URI를 사용할 수 있습니다. |
response_type |
응답에서 반환할 값의 유형입니다. OAuth 2.0 암시적
흐름에서 응답 유형은 항상 token입니다. |
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&response_type=token&user_locale=LOCALE
승인 엔드포인트가 로그인 요청을 처리하도록 하려면 다음을 수행합니다. 단계:
다음과 같이
client_id및redirect_uri값을 확인합니다. 의도하지 않았거나 잘못 구성된 클라이언트 앱에 액세스 권한을 부여하지 않도록 합니다.client_id가 등록된 클라이언트 ID와 일치하는지 확인합니다. 확인할 수 있습니다redirect_uri에서 지정한 URL인지 확인합니다. 매개변수의 형식은 다음과 같습니다.https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID https://oauth-redirect-sandbox.googleusercontent.com/r/YOUR_PROJECT_ID
사용자가 서비스에 로그인했는지 확인합니다. 사용자가 로그인하지 않은 경우 서비스의 로그인 또는 가입 절차를 완료합니다.
Google에서 API에 액세스하는 데 사용할 액세스 토큰을 생성합니다. 이 액세스 토큰은 임의의 문자열 값이 될 수 있지만 클라이언트와 클라이언트에게 전달하며 추측할 수 없어야 합니다.
사용자의 브라우저를 URL로 리디렉션하는 HTTP 응답 전송
redirect_uri매개변수로 지정됩니다. 다음을 모두 포함 다음 매개변수를 포함합니다.access_token: 방금 생성한 액세스 토큰입니다.token_type: 문자열bearerstate: 원본의 수정되지 않은 상태 값입니다. 요청
다음은 결과 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이
Google에서 후속 호출에 토큰을 첨부합니다.
서비스 API에 연결할 수 있습니다
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.
구현 확인
OAuth 2.0 Playground 도구를 사용하여 구현의 유효성을 검사할 수 있습니다.
도구에서 다음 단계를 수행합니다.
- 구성 을 클릭하여 OAuth 2.0 구성 창을 엽니다.
- OAuth 흐름 필드에서 클라이언트 측 을 선택합니다.
- OAuth 엔드포인트 필드에서 맞춤 을 선택합니다.
- 해당 필드에서 OAuth 2.0 엔드포인트와 Google에 할당한 클라이언트 ID를 지정합니다.
- 1단계 섹션에서 Google 범위를 선택하지 않습니다. 대신 이 필드를 비워 두거나 서버에 유효한 범위 (또는 OAuth 범위를 사용하지 않는 경우 임의의 문자열)를 입력합니다. 완료되면 API 승인 을 클릭합니다.
- 2단계 및 3단계 섹션에서 OAuth 2.0 흐름을 진행하고 각 단계가 의도한 대로 작동하는지 확인합니다.
Google 계정 연결 데모 도구를 사용하여 구현의 유효성을 검사할 수 있습니다.
도구에서 다음 단계를 수행합니다.
- Google 계정으로 로그인 버튼을 클릭합니다.
- 연결하려는 계정을 선택합니다.
- 서비스 ID를 입력합니다.
- 액세스 권한을 요청할 범위를 하나 이상 입력합니다(선택사항).
- 데모 시작 을 클릭합니다.
- 메시지가 표시되면 연결 요청에 동의하거나 거부할 수 있음을 확인합니다.
- 플랫폼으로 리디렉션되는지 확인합니다.