Google Identity Services is migrating to FedCM APIs. Follow the migration guide to review potential changes and avoid negative impacts for user sign-in to your website.
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
أضِف زر "تسجيل الدخول باستخدام حساب Google" إلى موقعك الإلكتروني لتتمكّن المستخدمون من الاشتراك في تطبيق الويب أو تسجيل الدخول إليه. استخدِم لغة HTML أو JavaScript لعرض الزر والعلامات لتعديل شكل الزر وحجمه ونصه وموضوعه.
بعد أن يختار المستخدم حسابًا على Google ويقدّم موافقته، تشارك Google
ملفه الشخصي باستخدام رمز JSON المميّز للويب (JWT). للحصول على نظرة عامة على الخطوات
المُتّبعة أثناء تسجيل الدخول وتجربة المستخدم، يُرجى الاطّلاع على آلية العمل.
يراجع قسم فهم الزر المخصّص الشروط المختلفة
والحالات التي تؤثّر في كيفية عرض الزر للمستخدمين.
.
المتطلبات الأساسية
اتّبِع الخطوات الموضّحة في الإعداد لضبط شاشة طلب الموافقة المتعلّقة ببروتوكول OAuth والحصول على معرّف عميل وتحميل مكتبة العميل.
عرض الأزرار
لعرض زر "تسجيل الدخول باستخدام حساب Google"، يمكنك اختيار HTML أو
JavaScript لعرض الزر على صفحة تسجيل الدخول:
HTML
عرض زر تسجيل الدخول باستخدام HTML، مع إعادة JWT إلى نقطة نهاية تسجيل الدخول في منصتك
يتم عرض العنصر الذي يحتوي على فئة g_id_signin كزر "تسجيل الدخول باستخدام حساب Google".
يتم تخصيص الزر حسب المَعلمات المقدَّمة في سمات البيانات.
لعرض زر "تسجيل الدخول باستخدام حساب Google" وميزة "تسجيل الدخول بنقرة واحدة" من Google One في الصفحة نفسها،
أزِل data-auto_prompt="false". ننصح بذلك لتقليل الصعوبات و
تحسين معدّلات تسجيل الدخول.
للحصول على القائمة الكاملة لسمات البيانات، اطّلِع على صفحة g_id_signin المرجعية
.
JavaScript
عرض زر تسجيل الدخول باستخدام JavaScript، مع عرض JWT لمعالج طلب الاستدعاء JavaScript في المتصفّح
<html>
<body>
<scriptsrc="https://accounts.google.com/gsi/client"async></script>
<script>
functionhandleCredentialResponse(response){console.log("Encoded JWT ID token: "+response.credential);}window.onload=function(){google.accounts.id.initialize({client_id:"YOUR_GOOGLE_CLIENT_ID"callback:handleCredentialResponse});google.accounts.id.renderButton(document.getElementById("buttonDiv"),{theme:"outline",size:"large"}// customization attributes);google.accounts.id.prompt();// also display the One Tap dialog}</script>
<divid="buttonDiv"></div>
</body>
</html>
يظهر العنصر المحدّد كمَعلمة أولى للعنصر renderButton على هيئة زر
تسجيل الدخول باستخدام حساب Google. في هذا المثال، يتم استخدام buttonDiv لعرض
الزرّ على الصفحة. يتم تخصيص الزر باستخدام السمات
المحدّدة في المَعلمة الثانية إلى renderButton.
للحدّ من الصعوبات التي يواجهها المستخدم، يتمّ استدعاء google.accounts.id.prompt() لعرض
One Tap كخيار ثانٍ لاستخدام الزرّ لتسجيل الاشتراك أو تسجيل الدخول.
تُحلِّل مكتبة GIS مستند HTML بحثًا عن العناصر التي تم ضبط سمة رقم التعريف فيها على
g_id_onload أو سمات الفئة التي تحتوي على g_id_signin. في حال العثور على عنصر مماثل، يتم عرض الزر باستخدام HTML، بغض النظر عمّا إذا كنت قد عرضت الزر أيضًا في JavaScript. لتجنُّب عرض الزر مرتين، ربما باستخدام مَعلمات متضاربة، لا تُدرِج عناصر HTML تطابق هذه الأسماء في صفحات HTML.
يوضّح المقتطف التالي من الرمز كيفية عرض لغة الزر باللغة الفرنسية
من خلال إضافة المَعلمة hl إلى عنوان URL لمكتبة العميل وضبط سمة
data-locale على French:
يوضّح المقتطف التالي من الرمز كيفية عرض لغة الزر باللغة الفرنسية
من خلال إضافة المَعلمة hl إلى عنوان URL لـ "مكتبة العميل" واستدعاء الأسلوب
google.accounts.id.renderButton مع ضبط المَعلمة locale على
الفرنسية:
بعد الحصول على موافقة المستخدم، تُرجع Google بيانات اعتماد رمز JSON المميّز للويب (JWT)
المعروفة باسم رمز التعريف، إما إلى متصفّح المستخدم أو مباشرةً إلى نقطة نهاية تسجيل الدخول
التي تستضيفها منصّتك.
يعتمد المكان الذي تختاره لتلقّي JWT على ما إذا كنت تعرض الزر باستخدام HTML أو JavaScript وما إذا كنت تستخدم وضع تجربة المستخدم للنوافذ المنبثقة أو إعادة التوجيه.
وضع النافذة المنبثقة
يؤدي استخدام وضع تجربة المستخدم popup إلى تنفيذ مسار تجربة المستخدم لتسجيل الدخول في نافذة منبثقة. وتعدّ هذه التجربة
بشكل عام أقل تدخلاً في تجربة المستخدمين، وهي وضع تجربة المستخدم التلقائي.
عند عرض الزر باستخدام:
HTML
يمكنك ضبط أيّ مما يلي:
data-callback لإعادة JWT إلى معالِج طلب إعادة الاتصال
data-login_uri لتوجيه Google لإرسال JWT مباشرةً إلى نقطة نهاية تسجيل الدخول
باستخدام طلب POST.
في حال ضبط كلتا القيمتَين، تكون data-callback لها الأولوية على
data-login_uri. قد يكون ضبط كلتا القيمتين مفيدًا عند استخدام معالج callback
لتصحيح الأخطاء.
JavaScript
يجب تحديد callback، لا يتيح وضع النافذة المنبثقة عمليات إعادة التوجيه
عند عرض الزر في JavaScript. في حال ضبطها، يتم تجاهل login_uri.
عند استخدام وضع تجربة المستخدم redirect، يتم تنفيذ تجربة تسجيل الدخول باستخدام إعادة توجيه الصفحات الكاملة في متصفّح المستخدم، وتُعيد Google رمز JWT مباشرةً إلى نقطة نهاية تسجيل الدخول باستخدام طلب POST.
هذه الطريقة أكثر تدخلاً في خصوصية المستخدمين بشكل عام، ولكنها تُعدّ
الأكثر أمانًا لتسجيل الدخول.
عند عرض الزر باستخدام:
في HTML، يمكنك اختياريًا ضبط data-login_uri على معرّف الموارد المتّصل لنقطة نهاية تسجيل الدخول.
يمكن لـ JavaScript ضبط login_uri اختياريًا على معرّف الموارد المنتظم لنقطة نهاية تسجيل الدخول.
في حال عدم تقديم قيمة، ستُعيد Google رمز JWT إلى عنوان URL للصفحة
الحالية.
معرّف الموارد المنتظم لنقطة نهاية تسجيل الدخول
استخدِم HTTPS ومعرّف موارد منتظم مطلق عند ضبط data-login_uri أو login_uri.
على سبيل المثال، https://example.com/path.
لا يُسمح باستخدام HTTP إلا عند استخدام localhost أثناء التطوير:
http://localhost/path.
تاريخ التعديل الأخير: 2025-05-01 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","easyToUnderstand","thumb-up"],["ساعَدني المحتوى في حلّ مشكلتي.","solvedMyProblem","thumb-up"],["غير ذلك","otherUp","thumb-up"]],[["لا يحتوي على المعلومات التي أحتاج إليها.","missingTheInformationINeed","thumb-down"],["الخطوات معقدة للغاية / كثيرة جدًا.","tooComplicatedTooManySteps","thumb-down"],["المحتوى قديم.","outOfDate","thumb-down"],["ثمة مشكلة في الترجمة.","translationIssue","thumb-down"],["مشكلة في العيّنات / التعليمات البرمجية","samplesCodeIssue","thumb-down"],["غير ذلك","otherDown","thumb-down"]],["تاريخ التعديل الأخير: 2025-05-01 (حسب التوقيت العالمي المتفَّق عليه)"],[[["\u003cp\u003eEasily add a "Sign In With Google" button to your website for user sign-up and sign-in, customizable with HTML or JavaScript.\u003c/p\u003e\n"],["\u003cp\u003eGoogle securely shares user profiles using a JWT (JSON Web Token) after consent.\u003c/p\u003e\n"],["\u003cp\u003eBefore implementation, set up your OAuth Consent Screen, obtain a Client ID, and load the client library.\u003c/p\u003e\n"],["\u003cp\u003eHandle the JWT credential either on the client-side using a callback or server-side with a designated login endpoint.\u003c/p\u003e\n"],["\u003cp\u003eChoose between popup or redirect UX modes, considering user experience and security trade-offs for credential handling.\u003c/p\u003e\n"]]],[],null,["# Display the Sign In With Google button\n\nAdd a Sign In With Google button to your site to enable users to sign-up or\nsign-in to your web app. Use either HTML or JavaScript to render the button and\nattributes to customize the button shape, size, text, and theme.\n\nAfter a user selects a Google Account and provides their consent, Google shares\nthe user profile using a JSON Web Token (JWT). For an overview of the steps\ninvolved during sign-in and user experience see [How it works](/identity/gsi/web/guides/overview#how_it_works).\n[Understand the personalized button](/identity/gsi/web/guides/personalized-button) reviews the different conditions and\nstates affecting how the button is displayed to users.\n| **Key Point:** Our [Code Generator](/identity/gsi/web/tools/configurator) helps you to interactively design your Sign In With Google button and generates the code needed to add it to your web page.\n\nPrerequisites\n-------------\n\nFollow the steps described in [Setup](/identity/gsi/web/guides/get-google-api-clientid) to configure your OAuth Consent\nScreen, obtain a client ID, and load the client library.\n\nButton rendering\n----------------\n\nTo display the Sign In With Google button, you may choose either HTML or\nJavaScript to render the button on your login page: \n\n### HTML\n\nRender the sign-in button using HTML, returning the JWT to your platform's\nlogin endpoint. \n\n \u003chtml\u003e\n \u003cbody\u003e\n \u003cscript src=\"https://accounts.google.com/gsi/client\" async\u003e\u003c/script\u003e\n \u003cdiv id=\"g_id_onload\"\n data-client_id=\"\u003cvar translate=\"no\"\u003eYOUR_GOOGLE_CLIENT_ID\u003c/var\u003e\"\n data-login_uri=\"\u003cvar translate=\"no\"\u003ehttps://your.domain/your_login_endpoint\u003c/var\u003e\"\n data-auto_prompt=\"false\"\u003e\n \u003c/div\u003e\n \u003cdiv class=\"g_id_signin\"\n data-type=\"standard\"\n data-size=\"large\"\n data-theme=\"outline\"\n data-text=\"sign_in_with\"\n data-shape=\"rectangular\"\n data-logo_alignment=\"left\"\u003e\n \u003c/div\u003e\n \u003cbody\u003e\n \u003c/html\u003e\n\nAn element with a `g_id_signin` class renders as a Sign In With Google button.\nThe button is customized by the parameters provided in the data attributes.\n\nTo display a Sign In With Google button and Google One Tap on the same page,\nremove `data-auto_prompt=\"false\"`. This is recommended to reduce friction and\nimprove sign-in rates.\n\nFor the full list of data attributes, see the [`g_id_signin` reference](/identity/gsi/web/reference/html-reference#element_with_class_g_id_signin)\npage\n\n### JavaScript\n\nRender the sign-in button using JavaScript, returning the JWT to the\nbrowser's JavaScript callback handler. \n\n \u003chtml\u003e\n \u003cbody\u003e\n \u003cscript src=\"https://accounts.google.com/gsi/client\" async\u003e\u003c/script\u003e\n \u003cscript\u003e\n function handleCredentialResponse(response) {\n console.log(\"Encoded JWT ID token: \" + response.credential);\n }\n window.onload = function () {\n google.accounts.id.initialize({\n client_id: \"\u003cvar translate=\"no\"\u003eYOUR_GOOGLE_CLIENT_ID\u003c/var\u003e\"\n callback: handleCredentialResponse\n });\n google.accounts.id.renderButton(\n document.getElementById(\"buttonDiv\"),\n { theme: \"outline\", size: \"large\" } // customization attributes\n );\n google.accounts.id.prompt(); // also display the One Tap dialog\n }\n \u003c/script\u003e\n \u003cdiv id=\"buttonDiv\"\u003e\u003c/div\u003e\n \u003c/body\u003e\n \u003c/html\u003e\n\nThe element specified as the first parameter to `renderButton` displays as a\nSign In With Google button. In this example `buttonDiv` is used to render\nthe button on the page. The button is customized using the attributes\nspecified in the second parameter to `renderButton`.\n\nTo minimize user friction `google.accounts.id.prompt()` is called to display\nOne Tap as a second alternative to using the button to sign-up or sign-in.\n\nThe GIS library parses the HTML doc for elements with an ID attribute set to\n`g_id_onload`, or class attributes containing `g_id_signin`. If a matching\nelement is found, the button is rendered using HTML, regardless if you've\nalso rendered the button in JavaScript. To avoid displaying the button\ntwice, possibly with conflicting parameters don't include HTML elements\nmatching these names in your HTML pages.\n| **Warning:** When using the JavaScript API, avoid using `g_id_onload` or `g_id_signin` in your HTML code.\n\n### Button Language\n\nThe button language is automatically determined by the browser's default\nlanguage or the Google session user's preference. You can also choose the\nlanguage manually by adding the `hl` parameter and language code to the src\ndirective when loading the library and by adding the optional data-locale or\nlocale parameter [data-locale](/identity/gsi/web/reference/html-reference#data-locale) in HTML or [locale](/identity/gsi/web/reference/html-reference#data-locale) in JavaScript. \n\n### HTML\n\nThe following code snippet shows how to display the button language in French\nby adding the `hl` parameter to the client library URL and by setting the\n`data-locale` attribute to French: \n\n \u003cscript src=\"https://accounts.google.com/gsi/client?hl=fr\" async\u003e\u003c/script\u003e\n \u003cdiv class=\"g_id_signin\" data-locale=\"fr\"\u003e\n \u003c/div\u003e\n\n### JavaScript\n\nThe following code snippet shows how to display the button language in French\nby adding the `hl` parameter to the client library URL and calling the\n`google.accounts.id.renderButton` method with the `locale` parameter set to\nFrench: \n\n \u003cscript src=\"https://accounts.google.com/gsi/client?hl=fr\" async\u003e\u003c/script\u003e\n \u003cscript\u003e\n google.accounts.id.renderButton(\n document.getElementById(\"buttonDiv\"),\n { locale: \"fr\" }\n );\n \u003c/script\u003e\n\nCredential handling\n-------------------\n\nAfter user consent is given, Google returns a JSON Web Token (JWT) credential\nknown as an ID token to either the user's browser, or directly to a login\nendpoint hosted by your platform.\n\nWhere you choose to receive the JWT depends upon if you render the button using\nHTML or JavaScript and if popup or redirect UX mode is used.\n\n### Popup mode\n\nUsing `popup` UX mode performs the sign-in UX flow in a pop-up window. This is\ngenerally a less intrusive experience for users and is the default UX mode.\n\nWhen rendering the button using: \n\n### HTML\n\nYou can set either:\n\n- `data-callback` to return the JWT to your callback handler, or\n- `data-login_uri` for Google to send the JWT directly to your login endpoint using a [POST request](/identity/gsi/web/reference/html-reference#server-side).\n\nIf both values are set, `data-callback` takes precedence over\n`data-login_uri`. Setting both values may be helpful when using a callback\nhandler for debugging.\n\n### JavaScript\n\nYou must specify a `callback`, popup mode does not support redirects\nwhen rending the button in JavaScript. If set, `login_uri` is ignored.\n\nSee [handle the returned credential response](/identity/gsi/web/guides/handle-credential-responses-js-functions) for more on decoding the\nJWT within your JS callback handler.\n\n### Redirect mode\n\nUsing `redirect` UX mode performs the sign-in UX flow using full page\nredirection of the user's browser, and Google returns the JWT directly to your\nlogin endpoint using a [POST request](/identity/gsi/web/reference/html-reference#server-side).\nThis is generally a more intrusive experience for users, but is considered to\nbe the most secure sign-in method.\n\nWhen rendering the button using:\n\n- **HTML** optionally set `data-login_uri` to the URI of your login endpoint.\n- **JavaScript** optionally set `login_uri` to the URI of your login endpoint.\n\nIf you don't provide a value, Google returns the JWT to the URI of the current\npage.\n\n### Your login endpoint URI\n\nUse HTTPS and an absolute URI when setting `data-login_uri` or `login_uri`.\nFor example, `https://example.com/path`.\n\nHTTP is only allowed when using localhost during development:\n`http://localhost/path`.\n\nRefer to [Use secure JavaScript origins and redirect URIs](/identity/protocols/oauth2/policies#secure-response-handling)\nfor a full description of Google's requirements and validation rules."]]