يتم ربط الحسابات باستخدام مسار رمز التفويض المتوافق مع معيار OAuth 2.0 المتّبع في المجال.
بروتوكول OAuth 2.1 وPKCE للوكلاء
بالنسبة إلى وكلاء الذكاء الاصطناعي غير الاحتفاظي الحالة ومسارات العرض المتعدّد، يُنصح بفرض استخدام OAuth 2.1.
- مفتاح الحماية لتبادل الرموز (PKCE): يجب استخدامه لتأمين مسار رمز التفويض، ما يمنع هجمات الاعتراض.
- بدون تدفق ضمني: يعرض التدفق الضمني رموز الدخول في عنوان URL، ما يشكّل خطرًا أمنيًا على بيئات الوكيل.
يجب أن تتيح خدمتك نقاط نهاية الترخيص وتبادل الرموز المميزة المتوافقة مع OAuth 2.0/2.1.
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. نقطة النهاية الأولى هي نقطة نهاية التفويض، وهي مسؤولة عن العثور على موافقة المستخدمين أو الحصول عليها للوصول إلى البيانات. تعرض نقطة نهاية التفويض واجهة مستخدم لتسجيل الدخول للمستخدمين الذين لم يسبق لهم تسجيل الدخول، وتسجّل الموافقة على الوصول المطلوب. نقطة النهاية الثانية هي نقطة نهاية تبادل الرموز المميّزة، وتُستخدَم للحصول على سلاسل مشفّرة، تُعرف باسم الرموز المميّزة، التي تمنح المستخدم إذن الوصول إلى خدمتك.
عندما يحتاج أحد تطبيقات Google إلى طلب إحدى واجهات برمجة التطبيقات في خدمتك، تستخدم Google نقطتَي النهاية هاتَين معًا للحصول على إذن من المستخدمين لطلب واجهات برمجة التطبيقات هذه نيابةً عنهم.
ربط حساب Google: مسار رمز تفويض OAuth
يوضّح مخطط التسلسل التالي التفاعلات بين المستخدم وGoogle ونقطتَي نهاية خدمتك.
الأدوار والمسؤوليات
يحدّد الجدول التالي أدوار ومسؤوليات الجهات الفاعلة في مسار OAuth لربط حساب Google. يُرجى العِلم أنّه في عملية ربط حساب Google، تعمل Google بصفتها عميل OAuth، بينما تعمل خدمتك بصفتها مزوّد الهوية/الخدمة.
| Actor / Component | GAL Role | Responsibilities |
|---|---|---|
| Google App / Server | OAuth Client | يبدأ المسار ويتلقّى رمز التفويض ويستبدله بـ رموز مميّزة ويخزّنها بشكل آمن للوصول إلى واجهات برمجة التطبيقات في خدمتك. |
| Your Authorization Endpoint | Authorization Server | يصادق على المستخدمين ويحصل على موافقتهم على مشاركة إذن الوصول إلى بياناتهم مع Google. |
| Your Token Exchange Endpoint | Authorization Server | يتحقّق من صحة رموز التفويض ورموز إعادة التحميل، ويصدر رموز الدخول إلى خادم Google. |
| Google Redirect URI | Callback Endpoint | يتلقّى عملية إعادة توجيه المستخدم من خدمة التفويض مع القيمتَين
code وstate. |
تتّبع جلسة مسار رمز تفويض OAuth 2.0 التي تبدأها Google المسار التالي:
- تفتح Google نقطة نهاية التفويض في متصفّح المستخدم. إذا بدأ المسار على جهاز صوتي فقط لتنفيذ إجراء، تنقل Google التنفيذ إلى هاتف.
- يسجّل المستخدم الدخول، إذا لم يسبق له تسجيل الدخول، ويمنح Google إذن الوصول إلى بياناته باستخدام واجهة برمجة التطبيقات، إذا لم يسبق له منح الإذن.
- تنشئ خدمتك رمز تفويض وتعيده إلى Google. لإجراء ذلك، أعِد توجيه متصفّح المستخدم إلى Google مع إرفاق رمز التفويض بالطلب.
- ترسل Google رمز التفويض إلى نقطة نهاية تبادل الرموز المميّزة، التي تتحقّق من صحة الرمز وتعرض رمز دخول والرمز المميز لإعادة التحميل. رمز الدخول هو رمز قصير الأمد تقبله خدمتك كبيانات اعتماد للوصول إلى واجهات برمجة التطبيقات. الرمز المميز لإعادة التحميل هو رمز طويل الأمد يمكن لـ Google تخزينه واستخدامه للحصول على رموز دخول جديدة عند انتهاء صلاحيتها.
- بعد أن يكمل المستخدم مسار ربط الحساب، يحتوي كل طلب لاحق يتم إرساله من Google على رمز دخول.
وصفة التنفيذ
اتّبِع الخطوات التالية لتنفيذ مسار رمز التفويض.
الخطوة 1: معالجة طلبات التفويض
عندما تبدأ Google عملية ربط الحساب، تعيد توجيه المستخدم إلى نقطة نهاية التفويض. للحصول على تفاصيل حول عقود البروتوكول ومتطلبات المَعلمات، يُرجى الاطّلاع على نقطة نهاية التفويض.
لمعالجة الطلب، نفِّذ الإجراءات التالية:
التحقّق من صحة الطلب:
- تأكَّد من أنّ
client_idيتطابق مع رقم تعريف العميل المخصّص لـ Google. - تأكَّد من أنّ
redirect_uriيتطابق مع عنوان URL المتوقّع لإعادة التوجيه من Google:none https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID https://oauth-redirect-sandbox.googleusercontent.com/r/YOUR_PROJECT_ID - تأكَّد من أنّ
response_typeهوcode.
- تأكَّد من أنّ
مصادقة المستخدم:
- تحقَّق مما إذا كان المستخدم قد سجّل الدخول إلى خدمتك.
- إذا لم يكن المستخدم قد سجّل الدخول، اطلب منه إكمال مسار تسجيل الدخول أو الاشتراك.
إنشاء رمز تفويض:
- أنشئ رمز تفويض فريدًا لا يمكن تخمينه مرتبطًا بالمستخدم والعميل.
- اضبط الرمز على انتهاء الصلاحية بعد 10 دقائق تقريبًا.
إعادة التوجيه إلى Google:
- أعِد توجيه المتصفّح إلى عنوان URL المقدَّم في
redirect_uri. - أضِف مَعلمات طلب البحث التالية:
code: رمز التفويض الذي أنشأته.state: قيمة الحالة غير المعدَّلة التي تم تلقّيها من Google.
- أعِد توجيه المتصفّح إلى عنوان URL المقدَّم في
الخطوة 2: معالجة طلبات تبادل الرموز المميّزة
تعالج نقطة نهاية تبادل الرموز المميّزة نوعَين من الطلبات: تبادل الرموز المميّزة مقابل الرموز، وتجديد رموز الدخول المنتهية الصلاحية. للحصول على تفاصيل حول عقود البروتوكول ومتطلبات المَعلمات، يُرجى الاطّلاع على نقطة نهاية تبادل الرموز المميّزة.
أ. تبادل رموز التفويض مقابل الرموز المميّزة
عندما تتلقّى Google رمز التفويض، تطلب نقطة نهاية تبادل الرموز المميّزة (POST) لاسترداد الرموز المميّزة.
التحقّق من صحة الطلب:
- تحقَّق من
client_idوclient_secret. - تأكَّد من أنّ رمز التفويض صالح ولم تنتهِ صلاحيته.
- تأكَّد من أنّ
redirect_uriيتطابق مع القيمة المستخدَمة في الخطوة 1. - إذا تعذّر التحقّق من الصحة، أعِد رمز HTTP
400 Bad Requestمع{"error": "invalid_grant"}.
- تحقَّق من
إصدار الرموز المميّزة:
- أنشئ
refresh_tokenطويل الأمد وaccess_tokenقصير الأمد (عادةً ساعة واحدة). - أعِد رمز HTTP
200 OKمع استجابة الرمز المميّز بتنسيق JSON القياسي.
- أنشئ
ب. تجديد رموز الدخول
عند انتهاء صلاحية رمز الدخول، تطلب Google رمزًا جديدًا باستخدام رمز إعادة التحميل.
التحقّق من صحة الطلب:
- تحقَّق من
client_idوclient_secretوrefresh_token. - إذا تعذّر التحقّق من الصحة، أعِد رمز HTTP
400 Bad Requestمع{"error": "invalid_grant"}.
- تحقَّق من
إصدار رمز دخول جديد:
- أنشئ
access_tokenجديدًا قصير الأمد. - أعِد رمز HTTP
200 OKمع استجابة الرمز المميّز بتنسيق JSON (بما في ذلك الرمز المميز لإعادة التحميل الجديد اختياريًا).
- أنشئ
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.
في الأداة، اتّبِع الخطوات التالية:
- انقر على الإعداد لفتح نافذة "إعدادات OAuth 2.0".
- في حقل مسار OAuth، اختَر من جهة العميل.
- في حقل نقاط نهاية OAuth، اختَر مخصّص.
- حدِّد نقطة نهاية OAuth 2.0 ومعرّف العميل الذي خصّصته لـ Google في الحقلَين المناسبَين.
- في قسم الخطوة 1، لا تحدّد أي نطاقات Google. بدلاً من ذلك، اترك هذا الحقل فارغًا أو اكتب نطاقًا صالحًا لخادمك (أو سلسلة عشوائية إذا كنت لا تستخدم نطاقات OAuth). عند الانتهاء، انقر على تفويض واجهات برمجة التطبيقات.
- في القسمَين الخطوة 2 والخطوة 3، اتّبِع مسار OAuth 2.0 وتأكَّد من أنّ كل خطوة تعمل على النحو المطلوب.
يمكنك التحقّق من صحة عملية التنفيذ باستخدام أداة عرض توضيحي لربط حساب Google.
في الأداة، اتّبِع الخطوات التالية:
- انقر على الزر تسجيل الدخول باستخدام حساب Google.
- اختَر الحساب الذي تريد ربطه.
- أدخِل رقم تعريف الخدمة.
- يمكنك اختياريًا إدخال نطاق واحد أو أكثر ستطلب الوصول إليه.
- انقر على بدء العرض التوضيحي.
- أكِّد أنّه يمكنك الموافقة على طلب ربط الحساب ورفضه عندما يُطلب منك ذلك.
- تأكَّد من إعادة توجيهك إلى منصتك.