يتم ربط الحسابات باستخدام التدفقات الضمنية والضمنية ورموز التفويض الخاصة ببروتوكول OAuth 2.0. يجب أن تتوافق خدمتك مع نقاط نهاية التفويض وتبادل الرموز المميّزة المتوافقة مع بروتوكول OAuth 2.0.
في عملية الموافقة الضمنية، تفتح Google نقطة نهاية التفويض في browser المستخدم. بعد تسجيل الدخول بنجاح، يمكنك إعادة رمز دخول صالح لفترة طويلة إلى Google. يتم الآن تضمين رمز الوصول هذا في كل طلب يتم إرساله من Google.
في مسار رمز التفويض، تحتاج إلى نقطتَي نهاية:
نقطة نهاية التفويض التي تعرِض واجهة مستخدم تسجيل الدخول للمستخدمين الذين لم يسجّلوا الدخول من قبل تُنشئ نقطة نهاية التفويض أيضًا код التفويض الذي ينتهي صلاحيته بعد فترة قصيرة لتسجيل موافقة المستخدمين على الوصول المطلوب.
نقطة نهاية تبادل الرمز المميز، وهي المسؤولة عن نوعين من التبادلات:
- تبديل رمز التفويض برمز مميّز طويل الأمد لإعادة التحميل ورمز مميّز قصير الأمد للدخول يحدث هذا التبادل عندما ينتقل المستخدم إلى مسار ربط الحساب.
- يتم استبدال رمز مميّز طويل الأمد لإعادة التحميل برمز دخول قصير الأمد. يحدث هذا التبادل عندما تحتاج Google إلى رمز دخول مميّز جديد لأنّه انتهت صلاحية الرمز المميّز السابق.
اختيار مسار OAuth 2.0
على الرغم من أنّ التدفق الضمني أبسط في التنفيذ، تنصح Google بعدم انتهاء صلاحية رموز الدخول الصادرة عن التدفق الضمني أبدًا. ويرجع ذلك إلى أن المستخدم يضطر إلى ربط حسابه مرة أخرى بعد انتهاء صلاحية الرمز المميز بالتدفق الضمني. إذا كنت بحاجة إلى انتهاء صلاحية الرمز المميّز لأسباب تتعلق بالأمان، ننصحك بشدة باستخدام مسار رمز التفويض بدلاً من ذلك.
إرشادات التصميم
يصف هذا القسم متطلبات التصميم والاقتراحات المتعلقة بشاشة المستخدم التي تستضيفها لمسارات ربط OAuth. بعد أن يطلب تطبيق Google الحصول على هذه الخدمة، تعرض منصتك صفحة تسجيل الدخول إلى Google وشاشة موافقة ربط الحساب للمستخدم. يتم توجيه المستخدم مرة أخرى إلى تطبيق Google بعد منح الموافقة على ربط الحسابات.
![يعرض هذا الشكل الخطوات التي يجب أن يتّبعها المستخدم لربط حسابه على Google بنظام المصادقة لديك. تعرض لقطة الشاشة الأولى
عمليات الربط التي يبدأها المستخدم من منصّتك. تعرض الصورة الثانية
تسجيل دخول المستخدم إلى Google، بينما تعرض الصورة الثالثة موافقة المستخدم وتأكيده على ربط حسابه على Google بتطبيقك. تعرِض
لقطة الشاشة الأخيرة حساب مستخدم تم ربطه بنجاح في
تطبيق Google.](https://developers-dot-devsite-v2-prod.appspot.com/static/identity/images/oauth-linking-flow.png?authuser=1&hl=ar)
المتطلبات
- عليك إبلاغنا بأنّ حساب المستخدم سيتم ربطه بخدمة Google، وليس بمنتج معيّن من Google، مثل Google Home أو "مساعد Google".
اقتراحات
ننصحك باتّباع الخطوات التالية:
عرض سياسة خصوصية Google: أدرِج رابطًا يؤدي إلى سياسة خصوصية Google على شاشة الموافقة.
البيانات التي ستتم مشاركتها: استخدِم لغة واضحة وموجزة لإعلام المستخدم بال data التي تطلبها Google منه والغرض من ذلك.
عبارة واضحة تحثّ على اتّخاذ إجراء: يجب تضمين عبارة واضحة للحث على اتّخاذ إجراء على شاشة طلب الموافقة، مثل "أوافق وأريد الربط". ويعود سبب ذلك إلى أنّ المستخدمين يحتاجون إلى معرفة البيانات التي سيُطلب منهم مشاركتها مع Google لربط حساباتهم.
إمكانية إلغاء الاشتراك: يجب توفير طريقة للمستخدمين للرجوع أو الإلغاء إذا اختَروا عدم الربط.
عملية تسجيل دخول واضحة: تأكَّد من توفُّر طريقة واضحة للمستخدمين لتسجيل الدخول إلى حساباتهم على Google، مثل حقول لاسم المستخدم وكلمة المرور أو تسجيل الدخول باستخدام حساب Google.
إمكانية إلغاء الربط: يجب توفير آلية تتيح للمستخدمين إلغاء ربط الحساب، مثل عنوان URL يؤدي إلى إعدادات حساباتهم على منصتك. بدلاً من ذلك، يمكنك تضمين رابط يؤدي إلى حساب Google حيث يمكن للمستخدمين إدارة حساباتهم المرتبطة.
القدرة على تغيير حساب المستخدم. اقترح طريقة للمستخدمين للتبديل بين حساباتهم. ويكون هذا مفيدًا بشكل خاص إذا كان المستخدمون يميلون إلى امتلاك حسابات متعددة.
- إذا كان على المستخدم إغلاق شاشة الموافقة لتبديل الحسابات، أرسِل خطأً قابلاً للاسترداد إلى Google حتى يتمكّن المستخدم من تسجيل الدخول إلى الحساب المطلوب باستخدام ربط OAuth ومسار الموافقة الضمنية.
أدرِج شعارك. عرض شعار شركتك على شاشة الموافقة استخدِم إرشادات الأنماط لوضع شعارك. إذا كنت ترغب أيضًا في عرض شعار Google، يمكنك الاطّلاع على الشعارات والعلامات التجارية.
![يعرض هذا الشكل مثالاً على شاشتَي تسجيل دخول المستخدم والموافقة مع
توضيحات للمتطلبات الفردية والاقتراحات التي يجب
اتّباعها عند تصميم شاشة تسجيل دخول المستخدم والموافقة.](https://developers-dot-devsite-v2-prod.appspot.com/static/identity/images/oauth-linking-consent.png?authuser=1&hl=ar)
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.
لعرض معرف المشروع الخاص بك:
- Go to the Google API Console.
- ابحث عن مشروعك في الجدول على الصفحة المقصودة. يظهر معرف المشروع في عمود المعرف .
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
To support the OAuth 2.0 implicit flow, your service makes an authorization endpoint available by HTTPS. This endpoint is responsible for authentication and 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.
When a Google application needs to call one of your service's authorized APIs, Google uses this endpoint to get permission from your users to call these APIs on their behalf.
A typical OAuth 2.0 implicit flow session initiated by Google has the following flow:
- Google opens your authorization endpoint in the user's browser. 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 access token and returns it to Google. To do so, redirect the user's browser back to Google with the access token attached to the request.
- Google calls your service's APIs and attaches the access token with each request. Your service verifies that the access token grants Google authorization to access the API and then completes the API call.
Handle authorization requests
When a Google application needs to perform account linking via an OAuth 2.0 implicit flow, Google sends the user to your authorization endpoint with a request that includes the following parameters:
Authorization endpoint parameters | |
---|---|
client_id |
The client ID you assigned to Google. |
redirect_uri |
The URL to which you send the response to this request. |
state |
A bookkeeping value that is passed back to Google unchanged in the redirect URI. |
response_type |
The type of value to return in the response. For the OAuth 2.0 implicit
flow, the response type is always token . |
user_locale |
The Google Account language setting in RFC5646 format used to localize your content in the user's preferred language. |
For example, if your authorization endpoint is available at
https://myservice.example.com/auth
, a request might look like the following:
GET https://myservice.example.com/auth?client_id=GOOGLE_CLIENT_ID&redirect_uri=REDIRECT_URI&state=STATE_STRING&response_type=token&user_locale=LOCALE
For your authorization endpoint to handle sign-in requests, do the following steps:
Verify the
client_id
andredirect_uri
values to prevent granting access to unintended or misconfigured client apps:- Confirm that the
client_id
matches the client ID you assigned to Google. - Confirm that the URL specified by the
redirect_uri
parameter has the following form:https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID https://oauth-redirect-sandbox.googleusercontent.com/r/YOUR_PROJECT_ID
- Confirm that the
Check if the user is signed in to your service. If the user isn't signed in, complete your service's sign-in or sign-up flow.
Generate an access token for Google to use to access your API. The access token can be any string value, but it must uniquely represent the user and the client the token is for and must not be guessable.
Send an HTTP response that redirects the user's browser to the URL specified by the
redirect_uri
parameter. Include all of the following parameters in the URL fragment:access_token
: The access token you just generatedtoken_type
: The stringbearer
state
: The unmodified state value from the original request
The following is an example of the resulting URL:
https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID#access_token=ACCESS_TOKEN&token_type=bearer&state=STATE_STRING
Google's OAuth 2.0 redirect handler receives the access token and confirms
that the state
value hasn't changed. After Google has obtained an
access token for your service, Google attaches the token to subsequent calls
to your service APIs.
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.
التحقّق من صحة عملية التنفيذ
يمكنك التحقّق من صحة التنفيذ باستخدام أداة ساحة اختبار OAuth 2.0.
في الأداة، اتّبِع الخطوات التالية:
- انقر على رمز الإعدادات لفتح نافذة ضبط OAuth 2.0.
- في حقل مسار OAuth، اختَر جانب العميل.
- في الحقل نقاط نهاية OAuth، اختَر مخصّص.
- حدِّد نقطة نهاية OAuth 2.0 ومعرّف العميل الذي عيّنته لخدمة Google في الحقول المقابلة.
- في قسم الخطوة 1، لا تختَر أيّ نطاقات Google. بدلاً من ذلك، اترك هذا الحقل فارغًا أو اكتب نطاقًا صالحًا لخادمك (أو سلسلة عشوائية إذا كنت لا تستخدم نطاقات OAuth). عند الانتهاء، انقر على تفويض واجهات برمجة التطبيقات.
- في القسمَين الخطوة 2 والخطوة 3، انتقِل إلى مسار OAuth 2.0 وتحقَّق من أنّ كل خطوة تعمل على النحو المطلوب.
يمكنك التحقّق من صحة عملية التنفيذ باستخدام أداة العرض التوضيحي لربط حساب Google.
في الأداة، اتّبِع الخطوات التالية:
- انقر على الزر تسجيل الدخول باستخدام حساب Google.
- اختَر الحساب الذي تريد ربطه.
- أدخِل رقم تعريف الخدمة.
- يمكنك اختياريًا إدخال نطاق واحد أو أكثر لطلب الوصول إليه.
- انقر على بدء العرض التجريبي.
- أكِّد أنّه يمكنك الموافقة على طلب ربط الحساب ورفضه عندما يُطلب منك ذلك.
- تأكَّد من إعادة توجيهك إلى منصّتك.