사용자가 웹 앱에 가입하거나 로그인할 수 있도록 사이트에 Google 계정으로 로그인 버튼을 추가합니다. HTML 또는 JavaScript를 사용하여 버튼을 렌더링하고 속성을 사용하여 버튼 모양, 크기, 텍스트, 테마를 맞춤설정합니다.
사용자가 Google 계정을 선택하고 동의하면 Google은 JSON 웹 토큰 (JWT)을 사용하여 사용자 프로필을 공유합니다. 로그인 및 사용자 환경 중에 발생하는 단계에 관한 개요는 작동 방식을 참고하세요.
맞춤설정된 버튼 이해하기에서는 버튼이 사용자에게 표시되는 방식에 영향을 미치는 다양한 조건과 상태를 검토합니다.
기본 요건
설정에 설명된 단계에 따라 OAuth 동의 화면을 구성하고, 클라이언트 ID를 가져오고, 클라이언트 라이브러리를 로드합니다.
버튼 렌더링
Google 계정으로 로그인 버튼을 표시하려면 HTML 또는 JavaScript 중 하나를 선택하여 로그인 페이지에 버튼을 렌더링할 수 있습니다.
HTML
HTML을 사용하여 로그인 버튼을 렌더링하여 JWT를 플랫폼의 로그인 엔드포인트로 반환합니다.
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()이 호출되어 버튼을 사용하여 가입하거나 로그인하는 것의 두 번째 대안으로 원탭을 표시합니다.
GIS 라이브러리는 ID 속성이 g_id_onload로 설정된 요소 또는 g_id_signin가 포함된 클래스 속성이 있는 HTML 문서를 파싱합니다. 일치하는 요소가 발견되면 JavaScript에서 버튼을 렌더링했는지와 관계없이 HTML을 사용하여 버튼이 렌더링됩니다. 버튼이 두 번 표시되지 않도록 하려면 충돌하는 매개변수가 있을 수 있으므로 HTML 페이지에 이러한 이름과 일치하는 HTML 요소를 포함하지 마세요.
버튼 언어
버튼 언어는 브라우저의 기본 언어 또는 Google 세션 사용자의 환경설정에 따라 자동으로 결정됩니다. 라이브러리를 로드할 때 src 디렉티브에 hl 매개변수와 언어 코드를 추가하고 HTML에 선택적 data-locale 또는 locale 매개변수 data-locale 또는 JavaScript에 locale를 추가하여 언어를 수동으로 선택할 수도 있습니다.
HTML
다음 코드 스니펫은 클라이언트 라이브러리 URL에 hl 매개변수를 추가하고 data-locale 속성을 프랑스어로 설정하여 버튼 언어를 프랑스어로 표시하는 방법을 보여줍니다.
redirect UX 모드를 사용하면 사용자 브라우저의 전체 페이지 리디렉션을 사용하여 로그인 UX 흐름이 실행되고 Google은 POST 요청을 사용하여 로그인 엔드포인트로 JWT를 직접 반환합니다.
일반적으로 사용자에게 더 방해가 되는 환경이지만 가장 안전한 로그인 방법으로 간주됩니다.
다음을 사용하여 버튼을 렌더링하는 경우:
HTML: 원하는 경우 data-login_uri를 로그인 엔드포인트의 URI로 설정합니다.
JavaScript: 원하는 경우 login_uri를 로그인 엔드포인트의 URI로 설정합니다.
값을 제공하지 않으면 Google에서 JWT를 현재 페이지의 URI로 반환합니다.
로그인 엔드포인트 URI
data-login_uri 또는 login_uri를 설정할 때 HTTPS 및 절대 URI를 사용합니다.
예를 들면 https://example.com/path입니다.
HTTP는 개발 중에 localhost(http://localhost/path)를 사용할 때만 허용됩니다.
[[["이해하기 쉬움","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(UTC)"],[[["\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."]]