حسابها با استفاده از جریان کد مجوز استاندارد صنعتی OAuth 2.0 به هم مرتبط میشوند.
OAuth 2.1 و PKCE برای نمایندگان
برای عاملهای هوش مصنوعی بدون تابعیت و خطوط لوله چندوجهی، اجرای OAuth 2.1 توصیه میشود.
- PKCE (کلید اثبات برای تبادل کد) : باید برای ایمنسازی جریان کد مجوز و جلوگیری از حملات رهگیری استفاده شود.
- بدون جریان ضمنی : جریان ضمنی، توکنهای دسترسی را در URL نمایش میدهد که یک خطر امنیتی برای محیطهای عامل است.
سرویس شما باید از نقاط پایانی مجوز و تبادل توکن سازگار با OAuth 2.0/2.1 پشتیبانی کند.
پروژه را ایجاد کنید
برای ایجاد پروژه خود با استفاده از پیوند حساب:
- به کنسول API گوگل بروید.
- روی ایجاد پروژه کلیک کنید.
- یک نام وارد کنید یا پیشنهاد تولید شده را بپذیرید.
- فیلدهای باقی مانده را تأیید یا ویرایش کنید.
- روی ایجاد کلیک کنید.
برای مشاهده شناسه پروژه خود:
- به کنسول API گوگل بروید.
- پروژه خود را در جدول صفحه اصلی پیدا کنید. شناسه پروژه در ستون شناسه نمایش داده میشود.
صفحه رضایت OAuth خود را پیکربندی کنید
فرآیند پیوند حساب گوگل شامل یک صفحه رضایتنامه است که به کاربران میگوید برنامهای که درخواست دسترسی به دادههای آنها را دارد، چه نوع دادههایی را درخواست میکند و شرایط اعمال آن چیست. قبل از ایجاد شناسه کلاینت Google API، باید صفحه رضایتنامه OAuth خود را پیکربندی کنید.
- صفحهی رضایتنامهی OAuth را در کنسول Google APIs باز کنید.
- در صورت درخواست، پروژهای را که تازه ایجاد کردهاید انتخاب کنید.
در صفحه «صفحه رضایت OAuth»، فرم را پر کنید و روی دکمه «ذخیره» کلیک کنید.
نام برنامه: نام برنامهای که درخواست رضایت میکند. این نام باید دقیقاً منعکسکننده برنامه شما باشد و با نام برنامهای که کاربران در جاهای دیگر میبینند، مطابقت داشته باشد. نام برنامه در صفحه رضایتنامه پیوند حساب نمایش داده خواهد شد.
لوگوی برنامه: تصویری در صفحه رضایتنامه که به کاربران کمک میکند برنامه شما را بشناسند. این لوگو در صفحه رضایتنامه پیوند حساب و در تنظیمات حساب نمایش داده میشود.
ایمیل پشتیبانی: برای اینکه کاربران بتوانند در مورد رضایت خود با شما تماس بگیرند.
حوزههای دسترسی برای APIهای گوگل: حوزهها به برنامه شما اجازه میدهند تا به دادههای خصوصی گوگل کاربر دسترسی داشته باشد. برای مورد استفاده اتصال حساب گوگل، حوزه پیشفرض (ایمیل، نمایه، شناسه باز) کافی است، نیازی به اضافه کردن هیچ حوزه حساسی ندارید. به طور کلی، بهترین روش این است که حوزهها را به صورت تدریجی، در زمانی که دسترسی مورد نیاز است، درخواست کنید، نه اینکه از ابتدا درخواست دهید. اطلاعات بیشتر .
دامنههای مجاز: برای محافظت از شما و کاربرانتان، گوگل فقط به برنامههایی که با استفاده از OAuth احراز هویت میشوند، اجازه استفاده از دامنههای مجاز را میدهد. لینکهای برنامههای شما باید در دامنههای مجاز میزبانی شوند. اطلاعات بیشتر .
لینک صفحه اصلی برنامه: صفحه اصلی برنامه شما. باید روی یک دامنه مجاز میزبانی شود.
پیوند سیاست حفظ حریم خصوصی برنامه: در صفحه رضایتنامه پیوند حساب گوگل نشان داده میشود. باید در یک دامنه مجاز میزبانی شود.
لینک شرایط خدمات برنامه (اختیاری): باید روی یک دامنه مجاز میزبانی شود.

شکل ۱. صفحه رضایتنامه اتصال حساب گوگل برای یک برنامه جعلی، Tunery
«وضعیت تأیید» را بررسی کنید، اگر درخواست شما نیاز به تأیید دارد، روی دکمه «ارسال برای تأیید» کلیک کنید تا درخواست شما برای تأیید ارسال شود. برای جزئیات بیشتر به الزامات تأیید OAuth مراجعه کنید.
سرور OAuth خود را پیادهسازی کنید
An OAuth 2.0 server implementation of the authorization code flow consists of two endpoints, which your service makes available by HTTPS. The first endpoint is the authorization endpoint, which is responsible for finding or obtaining consent from users for data access. The authorization endpoint presents a sign-in UI to your users that aren't already signed in and records consent to the requested access. The second endpoint is the token exchange endpoint, which is used to obtain encrypted strings, called tokens, that authorize a user to access your service.
When a Google application needs to call one of your service's APIs, Google uses these endpoints together to get permission from your users to call these APIs on their behalf.
Google Account Linking: OAuth Authorization Code Flow
The following sequence diagram details interactions between the User, Google, and your service's endpoints.
Roles and responsibilities
The following table defines the roles and responsibilities of the actors in the Google Account Linking (GAL) OAuth flow. Note that in GAL, Google acts as the OAuth Client, while your service acts as the Identity/Service Provider.
| Actor / Component | GAL Role | Responsibilities |
|---|---|---|
| Google App / Server | OAuth Client | Initiates the flow, receives the authorization code, exchanges it for tokens, and securely stores them to access your service's APIs. |
| Your Authorization Endpoint | Authorization Server | Authenticates your users and obtains their consent to share access to their data with Google. |
| Your Token Exchange Endpoint | Authorization Server | Validates authorization codes and refresh tokens, and issues access tokens to the Google Server. |
| Google Redirect URI | Callback Endpoint | Receives the user redirect from your authorization service with the
code and state values. |
An OAuth 2.0 authorization code flow session initiated by Google has the following flow:
- Google opens your authorization endpoint in the user's browser. If the flow started on a voice-only device for an Action, Google transfers the execution to a phone.
- The user signs in, if not signed in already, and grants Google permission to access their data with your API, if they haven't already granted permission.
- Your service creates an authorization code and returns it to Google. To do so, redirect the user's browser back to Google with the authorization code attached to the request.
- Google sends the authorization code to your token exchange endpoint, which verifies the authenticity of the code and returns an access token and a refresh token. The access token is a short-lived token that your service accepts as credentials to access APIs. The refresh token is a long-lived token that Google can store and use to acquire new access tokens when they expire.
- After the user has completed the account linking flow, every subsequent request sent from Google contains an access token.
Implementation Recipe
Follow these steps to implement the Authorization Code flow.
Step 1: Handle authorization requests
When Google initiates account linking, it redirects the user to your authorization endpoint. For detailed protocol contracts and parameter requirements, see the Authorization Endpoint.
To handle the request, perform the following actions:
Validate the request:
- Confirm that the
client_idmatches the Client ID assigned to Google. - Confirm that the
redirect_urimatches the expected Google redirect URL:none https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID https://oauth-redirect-sandbox.googleusercontent.com/r/YOUR_PROJECT_ID - Verify that
response_typeiscode.
- Confirm that the
Authenticate the user:
- Check if the user is signed in to your service.
- If the user is not signed in, prompt them to complete your sign-in or sign-up flow.
Generate authorization code:
- Create a unique, non-guessable authorization code associated with the user and client.
- Set the code to expire in approximately 10 minutes.
Redirect back to Google:
- Redirect the browser to the URL provided in
redirect_uri. - Append the following query parameters:
code: The authorization code you generated.state: The unmodified state value received from Google.
- Redirect the browser to the URL provided in
Step 2: Handle token exchange requests
Your token exchange endpoint processes two types of requests: exchanging codes for tokens, and refreshing expired access tokens. For detailed protocol contracts and parameter requirements, see the Token Exchange Endpoint.
A. Exchange authorization codes for tokens
When Google receives the authorization code, it calls your token exchange endpoint (POST) to retrieve tokens.
Validate the request:
- Verify
client_idandclient_secret. - Verify the authorization code is valid and not expired.
- Confirm
redirect_urimatches the value used in Step 1. - If validation fails, return an HTTP
400 Bad Requestwith{"error": "invalid_grant"}.
- Verify
Issue tokens:
- Generate a long-lived
refresh_tokenand a short-livedaccess_token(typically 1 hour). - Return an HTTP
200 OKwith the standard JSON token response.
- Generate a long-lived
B. Refresh access tokens
When the access token expires, Google requests a new one using the refresh token.
Validate the request:
- Verify
client_id,client_secret, andrefresh_token. - If validation fails, return an HTTP
400 Bad Requestwith{"error": "invalid_grant"}.
- Verify
Issue new access token:
- Generate a new short-lived
access_token. - Return an HTTP
200 OKwith the JSON token response (optionally including a new refresh token).
- Generate a new short-lived
رسیدگی به درخواست های اطلاعات کاربر
نقطه پایانی userinfo یک منبع محافظت شده OAuth 2.0 است که ادعاهای مربوط به کاربر پیوند شده را برمیگرداند. پیاده سازی و میزبانی نقطه پایانی اطلاعات کاربر اختیاری است، به جز موارد استفاده زیر:
- ورود به سیستم حساب پیوندی با Google One Tap.
- اشتراک بدون اصطکاک در AndroidTV.
پس از اینکه رمز دسترسی با موفقیت از نقطه پایانی نشانه شما بازیابی شد، Google درخواستی را به نقطه پایانی اطلاعات کاربری شما ارسال می کند تا اطلاعات نمایه اولیه کاربر پیوند داده شده را بازیابی کند.
| سرصفحه های درخواست نقطه پایانی کاربر | |
|---|---|
Authorization header | نشانه دسترسی از نوع Bearer. |
به عنوان مثال، اگر نقطه پایانی اطلاعات کاربری شما در https://myservice.example.com/userinfo در دسترس باشد، ممکن است یک درخواست به شکل زیر باشد:
GET /userinfo HTTP/1.1 Host: myservice.example.com Authorization: Bearer ACCESS_TOKEN
برای اینکه نقطه پایانی اطلاعات کاربری شما به درخواستها رسیدگی کند، مراحل زیر را انجام دهید:
- رمز دسترسی را از سربرگ Authorization استخراج کنید و اطلاعات کاربر مرتبط با نشانه دسترسی را برگردانید.
- اگر رمز دسترسی نامعتبر است، با استفاده از سربرگ پاسخ
WWW-Authenticateخطای غیرمجاز HTTP 401 را برگردانید. در زیر نمونه ای از پاسخ خطای userinfo آورده شده است: اگر یک پاسخ خطای 401 غیرمجاز یا هر پاسخ خطای ناموفق دیگری در طول فرآیند پیوند داده شود، خطا غیرقابل بازیابی خواهد بود، رمز بازیابی شده کنار گذاشته می شود و کاربر باید دوباره فرآیند پیوند را آغاز کند.HTTP/1.1 401 Unauthorized WWW-Authenticate: error="invalid_token", error_description="The Access Token expired"
اگر نشانه دسترسی معتبر است، پاسخ HTTP 200 را با شی JSON زیر در بدنه پاسخ HTTPS برگردانید:
اگر نقطه پایانی اطلاعات کاربری شما یک پاسخ موفقیت آمیز HTTP 200 برگرداند، رمز بازیابی شده و ادعاها در برابر حساب Google کاربر ثبت می شود.{ "sub": "USER_UUID", "email": "EMAIL_ADDRESS", "given_name": "FIRST_NAME", "family_name": "LAST_NAME", "name": "FULL_NAME", "picture": "PROFILE_PICTURE", }پاسخ نقطه پایانی اطلاعات کاربر subیک شناسه منحصر به فرد که کاربر را در سیستم شما شناسایی می کند. emailآدرس ایمیل کاربر. given_nameاختیاری: نام کاربر. family_nameاختیاری: نام خانوادگی کاربر. nameاختیاری: نام کامل کاربر. pictureاختیاری: تصویر نمایه کاربر.
اعتبارسنجی پیادهسازی شما
شما میتوانید پیادهسازی خود را با استفاده از ابزار OAuth 2.0 Playground اعتبارسنجی کنید.
در ابزار، مراحل زیر را انجام دهید:
- برای باز کردن پنجره پیکربندی OAuth 2.0، پیکربندی کلیک کنید.
- در فیلد جریان OAuth ، گزینه Client-side را انتخاب کنید.
- در فیلد OAuth Endpoints ، گزینه Custom را انتخاب کنید.
- نقطه پایانی OAuth 2.0 خود و شناسه کلاینتی که به گوگل اختصاص دادهاید را در فیلدهای مربوطه مشخص کنید.
- در بخش مرحله ۱ ، هیچ محدوده گوگلی را انتخاب نکنید. در عوض، این فیلد را خالی بگذارید یا یک محدوده معتبر برای سرور خود تایپ کنید (یا اگر از محدودههای OAuth استفاده نمیکنید، یک رشته دلخواه). وقتی کارتان تمام شد، روی Authorize APIs کلیک کنید.
- در بخشهای مرحله ۲ و مرحله ۳ ، جریان OAuth 2.0 را بررسی کنید و تأیید کنید که هر مرحله طبق برنامه عمل میکند.
شما میتوانید پیادهسازی خود را با استفاده از ابزار Google Account Linking Demo اعتبارسنجی کنید.
در ابزار، مراحل زیر را انجام دهید:
- روی دکمه ورود با گوگل کلیک کنید.
- حسابی را که میخواهید پیوند دهید انتخاب کنید.
- شناسه سرویس را وارد کنید.
- به صورت اختیاری یک یا چند محدودهای را که برای دسترسی به آنها درخواست خواهید کرد، وارد کنید.
- روی شروع نسخه نمایشی کلیک کنید.
- وقتی از شما خواسته شد، تأیید کنید که میتوانید درخواست پیوند را تأیید یا رد کنید.
- تأیید کنید که به پلتفرم خود هدایت میشوید.