حسابها با استفاده از جریانهای کد ضمنی و مجوزدهی استاندارد صنعتی 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 از جریان کد مجوز شامل دو نقطه پایانی است که سرویس شما از طریق HTTPS در دسترس قرار میدهد. نقطه پایانی اول، نقطه پایانی مجوز است که مسئول یافتن یا اخذ رضایت از کاربران برای دسترسی به دادهها است. نقطه پایانی مجوز، یک رابط کاربری ورود به سیستم را به کاربرانی که قبلاً وارد سیستم نشدهاند ارائه میدهد و رضایت آنها را برای دسترسی درخواستی ثبت میکند. نقطه پایانی دوم، نقطه پایانی تبادل توکن است که برای دریافت رشتههای رمزگذاری شده، به نام توکنها، که به کاربر اجازه دسترسی به سرویس شما را میدهند، استفاده میشود.
وقتی یک برنامه گوگل نیاز به فراخوانی یکی از APIهای سرویس شما دارد، گوگل از این نقاط پایانی به صورت مشترک استفاده میکند تا از کاربران شما اجازه فراخوانی این APIها را از طرف آنها دریافت کند.
لینک کردن حساب گوگل: جریان کد مجوز OAuth
نمودار توالی زیر، جزئیات تعاملات بین کاربر، گوگل و نقاط پایانی سرویس شما را نشان میدهد.
نقشها و مسئولیتها
جدول زیر نقشها و مسئولیتهای بازیگران در جریان OAuth اتصال حساب گوگل (GAL) را تعریف میکند. توجه داشته باشید که در GAL، گوگل به عنوان کلاینت OAuth عمل میکند، در حالی که سرویس شما به عنوان ارائهدهنده هویت/سرویس عمل میکند.
| بازیگر / جزء | نقش GAL | مسئولیتها |
|---|---|---|
| برنامه/سرور گوگل | کلاینت OAuth | جریان را آغاز میکند، کد مجوز را دریافت میکند، آن را با توکنها مبادله میکند و آنها را به طور ایمن ذخیره میکند تا به APIهای سرویس شما دسترسی داشته باشد. |
| نقطه پایانی مجوز شما | سرور احراز هویت | کاربران شما را احراز هویت میکند و رضایت آنها را برای اشتراکگذاری دسترسی به دادههایشان با گوگل دریافت میکند. |
| نقطه پایانی تبادل توکن شما | سرور احراز هویت | کدهای مجوز و توکنهای بهروزرسانی را اعتبارسنجی میکند و توکنهای دسترسی را به سرور گوگل صادر میکند. |
| آدرس اینترنتی ریدایرکت گوگل | نقطه پایانی پاسخ به تماس | تغییر مسیر کاربر را از سرویس احراز هویت شما به همراه code و مقادیر state دریافت میکند. |
یک جلسه جریان کد مجوز OAuth 2.0 که توسط گوگل آغاز شده است، جریان زیر را دارد:
- گوگل نقطه پایانی احراز هویت شما را در مرورگر کاربر باز میکند. اگر جریان برای یک اقدام در دستگاهی که فقط با صدا کار میکند شروع شده باشد، گوگل اجرا را به تلفن منتقل میکند.
- کاربر، اگر قبلاً وارد سیستم نشده باشد، وارد سیستم میشود و اگر قبلاً اجازه نداده باشد، به گوگل اجازه میدهد تا به دادههایش با API شما دسترسی پیدا کند.
- سرویس شما یک کد مجوز ایجاد میکند و آن را به گوگل برمیگرداند. برای انجام این کار، مرورگر کاربر را به همراه کد مجوز پیوست شده به درخواست، به گوگل هدایت کنید.
- گوگل کد مجوز را به نقطه پایانی تبادل توکن شما ارسال میکند، که صحت کد را تأیید میکند و یک توکن دسترسی و یک توکن بهروزرسانی را برمیگرداند. توکن دسترسی یک توکن کوتاهمدت است که سرویس شما آن را به عنوان اعتبارنامه برای دسترسی به APIها میپذیرد. توکن بهروزرسانی یک توکن بلندمدت است که گوگل میتواند آن را ذخیره کند و از آن برای به دست آوردن توکنهای دسترسی جدید پس از انقضا استفاده کند.
- پس از اینکه کاربر فرآیند اتصال حساب کاربری را تکمیل کرد، هر درخواست بعدی که از گوگل ارسال میشود حاوی یک توکن دسترسی است.
رسیدگی به درخواستهای مجوز
وقتی نیاز دارید که با استفاده از جریان کد مجوز OAuth 2.0، اتصال حساب کاربری را انجام دهید، گوگل کاربر را با درخواستی که شامل پارامترهای زیر است به نقطه پایانی مجوز شما ارسال میکند:
| پارامترهای نقطه پایانی احراز هویت | |
|---|---|
client_id | شناسه کلاینتی که به گوگل اختصاص دادهاید. |
redirect_uri | آدرس اینترنتی (URL) که پاسخ این درخواست را به آن ارسال میکنید. |
state | یک مقدار حسابداری که بدون تغییر در URL تغییر مسیر به گوگل بازگردانده میشود. |
scope | اختیاری: مجموعهای از رشتههای دامنه که با فاصله از هم جدا شدهاند و دادههایی را که گوگل برای آنها درخواست مجوز میکند، مشخص میکنند. |
response_type | نوع مقداری که در پاسخ برگردانده میشود. برای جریان کد احراز هویت OAuth 2.0، نوع پاسخ همیشه code است. |
user_locale | تنظیمات زبان حساب گوگل در قالب 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با Client ID که به گوگل اختصاص دادهاید مطابقت دارد وredirect_uriبا URL تغییر مسیر ارائه شده توسط گوگل برای سرویس شما مطابقت دارد. این بررسیها برای جلوگیری از اعطای دسترسی به برنامههای کلاینت ناخواسته یا پیکربندی نادرست مهم هستند. اگر از چندین جریان OAuth 2.0 پشتیبانی میکنید، همچنین تأیید کنید کهresponse_typecodeاست. - بررسی کنید که آیا کاربر وارد سرویس شما شده است یا خیر. اگر کاربر وارد نشده است، مراحل ورود یا ثبتنام سرویس خود را تکمیل کنید.
- یک کد مجوز برای دسترسی گوگل به API خود ایجاد کنید. کد مجوز میتواند هر مقدار رشتهای باشد، اما باید به طور منحصر به فرد، کاربر، کلاینتی که توکن برای آن است و زمان انقضای کد را نشان دهد و نباید قابل حدس زدن باشد. شما معمولاً کدهای مجوزی صادر میکنید که تقریباً پس از 10 دقیقه منقضی میشوند.
- تأیید کنید که URL مشخص شده توسط پارامتر
redirect_uriبه شکل زیر باشد:https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID https://oauth-redirect-sandbox.googleusercontent.com/r/YOUR_PROJECT_ID
- مرورگر کاربر را به آدرس اینترنتی مشخص شده توسط پارامتر
redirect_uriهدایت کنید. هنگام هدایت، با افزودن پارامترهایcodeوstate، کد مجوزی که اخیراً ایجاد کردهاید و مقدار وضعیت اصلی و بدون تغییر را نیز وارد کنید. در زیر نمونهای از آدرس اینترنتی حاصل آمده است:https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID?code=AUTHORIZATION_CODE&state=STATE_STRING
رسیدگی به درخواستهای تبادل توکن
نقطه پایانی تبادل توکن سرویس شما مسئول دو نوع تبادل توکن است:
- کدهای مجوز را برای توکنهای دسترسی و توکنهای بهروزرسانی مبادله کنید
- توکنهای بهروزرسانی را با توکنهای دسترسی مبادله کنید
درخواستهای مبادله توکن شامل پارامترهای زیر است:
| پارامترهای نقطه پایانی تبادل توکن | |
|---|---|
client_id | رشتهای که مبدا درخواست را گوگل معرفی میکند. این رشته باید در سیستم شما به عنوان شناسه منحصر به فرد گوگل ثبت شود. |
client_secret | یک رشته مخفی که شما برای سرویس خود در گوگل ثبت کردهاید. |
grant_type | نوع توکنی که رد و بدل میشود. این توکن میتواند authorization_code یا refresh_token باشد. |
code | وقتی grant_type=authorization_code ، این پارامتر کدی است که گوگل از نقطه پایانی ورود یا تبادل توکن شما دریافت کرده است. |
redirect_uri | وقتی grant_type=authorization_code ، این پارامتر همان URL مورد استفاده در درخواست مجوز اولیه است. |
refresh_token | وقتی grant_type=refresh_token ، این پارامتر توکن تازهسازی است که گوگل از نقطه پایانی تبادل توکن شما دریافت کرده است. |
کدهای مجوز را برای توکنهای دسترسی و توکنهای بهروزرسانی مبادله کنید
پس از اینکه کاربر وارد سیستم شد و نقطه پایانی مجوز شما یک کد مجوز کوتاهمدت را به گوگل برگرداند، گوگل درخواستی را به نقطه پایانی تبادل توکن شما ارسال میکند تا کد مجوز را با یک توکن دسترسی و یک توکن بهروزرسانی مبادله کند.
برای این درخواستها، مقدار grant_type authorization_code است و مقدار code برابر با کد مجوزی است که قبلاً به گوگل اعطا کردهاید. در زیر مثالی از درخواست برای تعویض کد مجوز با یک access token و یک refresh token آمده است:
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با مقدار مورد انتظار مطابقت دارد. - تأیید کنید که کد مجوز معتبر و منقضی نشده است و شناسه مشتری مشخص شده در درخواست با شناسه مشتری مرتبط با کد مجوز مطابقت دارد.
- تأیید کنید که URL مشخص شده توسط پارامتر
redirect_uriبا مقداری که در درخواست مجوز اولیه استفاده شده است، یکسان باشد. - اگر نمیتوانید تمام معیارهای قبلی را تأیید کنید، خطای HTTP 400 Bad Request را با
{"error": "invalid_grant"}به عنوان بدنه برگردانید. - در غیر این صورت، از شناسه کاربری از کد مجوز برای تولید یک توکن بهروزرسانی و یک توکن دسترسی استفاده کنید. این توکنها میتوانند هر مقدار رشتهای باشند، اما باید به طور منحصر به فرد، کاربر و کلاینتی را که توکن برای آن است، نشان دهند و نباید قابل حدس زدن باشند. برای توکنهای دسترسی، زمان انقضای توکن را نیز ثبت کنید، که معمولاً یک ساعت پس از صدور توکن است. توکنهای بهروزرسانی منقضی نمیشوند.
- شیء JSON زیر را در بدنه پاسخ HTTPS برگردانید:
{ "token_type": "Bearer", "access_token": "ACCESS_TOKEN", "refresh_token": "REFRESH_TOKEN", "expires_in": SECONDS_TO_EXPIRATION }
گوگل توکن دسترسی و توکن بهروزرسانی را برای کاربر ذخیره میکند و تاریخ انقضای توکن دسترسی را ثبت میکند. وقتی توکن دسترسی منقضی میشود، گوگل از توکن بهروزرسانی برای دریافت یک توکن دسترسی جدید از نقطه پایانی تبادل توکن شما استفاده میکند.
توکنهای بهروزرسانی را با توکنهای دسترسی مبادله کنید
وقتی یک توکن دسترسی منقضی میشود، گوگل درخواستی را به نقطه پایانی تبادل توکن شما ارسال میکند تا یک توکن بهروزرسانی را با یک توکن دسترسی جدید مبادله کند.
برای این درخواستها، مقدار grant_type refresh_token است و مقدار refresh_token برابر با مقدار توکن refresh است که قبلاً به گوگل اعطا کردهاید. در زیر مثالی از درخواست برای تعویض توکن refresh با توکن access آمده است:
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مبدا درخواست را گوگل معرفی میکند وclient_secretبا مقدار مورد انتظار مطابقت دارد. - تأیید کنید که توکن بهروزرسانی معتبر است و شناسهی کلاینت مشخصشده در درخواست با شناسهی کلاینت مرتبط با توکن بهروزرسانی مطابقت دارد.
- اگر نمیتوانید تمام معیارهای قبلی را تأیید کنید، خطای HTTP 400 Bad Request را با
{"error": "invalid_grant"}به عنوان بدنه برگردانید. - در غیر این صورت، از شناسه کاربریِ توکنِ بهروزرسانی برای تولید یک توکن دسترسی استفاده کنید. این توکنها میتوانند هر مقدار رشتهای باشند، اما باید بهطور منحصربهفرد، کاربر و کلاینتی را که توکن برای آن است، نشان دهند و نباید قابل حدس زدن باشند. برای توکنهای دسترسی، زمان انقضای توکن را نیز ثبت کنید، که معمولاً یک ساعت پس از صدور توکن است.
- شیء JSON زیر را در بدنه پاسخ HTTPS برگردانید:
{ "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-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.
اعتبارسنجی پیادهسازی شما
You can validate your implementation by using the OAuth 2.0 Playground tool.
In the tool, do the following steps:
- Click Configuration to open the OAuth 2.0 Configuration window.
- In the OAuth flow field, select Client-side.
- In the OAuth Endpoints field, select Custom.
- Specify your OAuth 2.0 endpoint and the client ID you assigned to Google in the corresponding fields.
- In the Step 1 section, don't select any Google scopes. Instead, leave this field blank or type a scope valid for your server (or an arbitrary string if you don't use OAuth scopes). When you're done, click Authorize APIs.
- In the Step 2 and Step 3 sections, go through the OAuth 2.0 flow and verify that each step works as intended.
You can validate your implementation by using the Google Account Linking Demo tool.
In the tool, do the following steps:
- Click the Sign in with Google button.
- Choose the account you'd like to link.
- Enter the service ID.
- Optionally enter one or more scopes that you will request access for.
- Click Start Demo.
- When prompted, confirm that you may consent and deny the linking request.
- Confirm that you are redirected to your platform.