<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 的第二个参数中指定的属性自定义按钮。
GIS 库会解析 HTML 文档,查找 ID 属性设为 g_id_onload 或类属性包含 g_id_signin 的元素。如果找到匹配的元素,系统会使用 HTML 渲染按钮,无论您是否还在 JavaScript 中渲染了按钮。为避免显示两次按钮(可能具有冲突的参数),请勿在 HTML 网页中添加与这些名称匹配的 HTML 元素。
按钮语言
按钮语言由浏览器的默认语言或 Google 会话用户的偏好设置自动确定。您还可以通过以下方式手动选择语言:在加载库时将 hl 参数和语言代码添加到 src 指令中,并在 HTML 中添加可选的 data-locale 或 locale 参数 data-locale,或在 JavaScript 中添加 locale。
[null,null,["最后更新时间 (UTC):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,["Add 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\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\nTo display the Sign In With Google button, you may choose either HTML or\nJavaScript to render the button on your login page: \n\nHTML\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\nJavaScript\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\nButton 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\nHTML\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\nJavaScript\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\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\nPopup 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\nHTML\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\nJavaScript\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\nRedirect 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\nYour 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."]]