Creazione di un pulsante Accedi con Google personalizzato
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Personalizzazione del pulsante di accesso con rendering automatico (consigliato)
Per creare un pulsante di accesso con Google con impostazioni personalizzate, aggiungi un elemento per contenere il pulsante di accesso alla pagina di accesso, scrivi una funzione che chiami signin2.render() con le impostazioni di stile e ambito e includi lo script https://apis.google.com/js/platform.js con la stringa di query onload=YOUR_RENDER_FUNCTION.
Di seguito è riportato un esempio di un pulsante Accedi con Google che specifica parametri di stile personalizzati:
Il seguente codice HTML, JavaScript e CSS genera il pulsante riportato sopra:
Puoi anche specificare le impostazioni per un pulsante di accesso a Google personalizzato definendo gli attributi data- per un elemento div con la classe g-signin2. Ad esempio:
Puoi creare un pulsante Accedi con Google in base al design del tuo sito.
Devi seguire le linee guida per il branding e utilizzare
i colori e le icone appropriati nel pulsante. Le linee guida per il branding forniscono inoltre asset icone che puoi utilizzare per progettare il pulsante. Inoltre, devi assicurarti che il pulsante sia in evidenza come le altre opzioni di accesso di terze parti.
Di seguito è riportato un esempio di pulsante Accedi con Google creato con un'immagine personalizzata:
Il seguente codice HTML, JavaScript e CSS genera il pulsante riportato sopra:
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Mancano le informazioni di cui ho bisogno","missingTheInformationINeed","thumb-down"],["Troppo complicato/troppi passaggi","tooComplicatedTooManySteps","thumb-down"],["Obsoleti","outOfDate","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Problema relativo a esempi/codice","samplesCodeIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-07-25 UTC."],[[["\u003cp\u003eThe Google Sign-In JavaScript library is deprecated; review the Deprecation and Sunset guide for migration details.\u003c/p\u003e\n"],["\u003cp\u003eYou can customize the appearance of the Google Sign-In button using the \u003ccode\u003esignin2.render()\u003c/code\u003e function with various style options.\u003c/p\u003e\n"],["\u003cp\u003eAlternatively, define \u003ccode\u003edata-\u003c/code\u003e attributes on a \u003ccode\u003ediv\u003c/code\u003e element with the class \u003ccode\u003eg-signin2\u003c/code\u003e for basic button customization.\u003c/p\u003e\n"],["\u003cp\u003eTo create a fully custom button, adhere to Google's branding guidelines, utilize provided assets, and ensure prominence comparable to other login options.\u003c/p\u003e\n"],["\u003cp\u003eFedCM APIs will be required for the Google Sign-In library, so conduct an impact assessment to ensure continued functionality.\u003c/p\u003e\n"]]],[],null,["# Building a custom Google Sign-In button\n\n| **Warning:** The Google Sign-In library optionally uses FedCM APIs, and their use will become a requirement. [Conduct an impact assessment](/identity/sign-in/web/gsi-with-fedcm) to confirm that user sign-in continues to function as expected. \n|\n| Support for the Google Sign-In library is deprecated, see the [Deprecation and Sunset](/identity/sign-in/web/deprecation-and-sunset) guide for more.\n\nCustomizing the automatically rendered sign-in button (recommended)\n-------------------------------------------------------------------\n\nTo create a Google Sign-In button with custom settings, add\nan element to contain the sign-in button to your sign-in page, write a function\nthat calls\n[`signin2.render()`](/identity/sign-in/web/reference#gapisignin2renderid_options)\nwith your style and scope settings,\nand include the `https://apis.google.com/js/platform.js` script\nwith the query string `onload=YOUR_RENDER_FUNCTION`.\n\nThe following is an example of a Google Sign-In button that specifies\ncustom style parameters:\n\nThe following HTML, JavaScript, and CSS code produces the button above: \n\n```gdscript\n\u003chtml\u003e\n\u003chead\u003e\n \u003cmeta name=\"google-signin-client_id\" content=\"YOUR_CLIENT_ID.apps.googleusercontent.com\"\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n \u003cdiv id=\"my-signin2\"\u003e\u003c/div\u003e\n \u003cscript\u003e\n function onSuccess(googleUser) {\n console.log('Logged in as: ' + googleUser.getBasicProfile().getName());\n }\n function onFailure(error) {\n console.log(error);\n }\n function renderButton() {\n gapi.signin2.render('my-signin2', {\n 'scope': 'profile email',\n 'width': 240,\n 'height': 50,\n 'longtitle': true,\n 'theme': 'dark',\n 'onsuccess': onSuccess,\n 'onfailure': onFailure\n });\n }\n \u003c/script\u003e\n\n \u003cscript src=\"https://apis.google.com/js/platform.js?onload=renderButton\" async defer\u003e\u003c/script\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\nYou can also specify settings for a custom Google Sign-In button by defining\n`data-` attributes to a `div` element with the class `g-signin2`. For example: \n\n \u003cdiv class=\"g-signin2\" data-width=\"300\" data-height=\"200\" data-longtitle=\"true\"\u003e\n\nBuilding a button with a custom graphic\n---------------------------------------\n\nYou can build a Google Sign-In button to fit your site's design.\nYou must follow the [branding guidelines](/identity/branding-guidelines) and use\nthe appropriate colors and icons in your button. The branding guidelines also\nprovide icon assets that you can use to design your button. You must also\nensure that your button is as prominent as other third-party login options.\n\nThe following is an example of a Google Sign-In button built with a custom\ngraphic:\n\nThe following HTML, JavaScript, and CSS code produces the button above: \n\n```gdscript\n\u003chtml\u003e\n\u003chead\u003e\n \u003clink href=\"https://fonts.googleapis.com/css?family=Roboto\" rel=\"stylesheet\" type=\"text/css\"\u003e\n \u003cscript src=\"https://apis.google.com/js/api:client.js\"\u003e\u003c/script\u003e\n \u003cscript\u003e\n var googleUser = {};\n var startApp = function() {\n gapi.load('auth2', function(){\n // Retrieve the singleton for the GoogleAuth library and set up the client.\n auth2 = gapi.auth2.init({\n client_id: 'YOUR_CLIENT_ID.apps.googleusercontent.com',\n cookiepolicy: 'single_host_origin',\n // Request scopes in addition to 'profile' and 'email'\n //scope: 'additional_scope'\n });\n attachSignin(document.getElementById('customBtn'));\n });\n };\n\n function attachSignin(element) {\n console.log(element.id);\n auth2.attachClickHandler(element, {},\n function(googleUser) {\n document.getElementById('name').innerText = \"Signed in: \" +\n googleUser.getBasicProfile().getName();\n }, function(error) {\n alert(JSON.stringify(error, undefined, 2));\n });\n }\n \u003c/script\u003e\n \u003cstyle type=\"text/css\"\u003e\n #customBtn {\n display: inline-block;\n background: white;\n color: #444;\n width: 190px;\n border-radius: 5px;\n border: thin solid #888;\n box-shadow: 1px 1px 1px grey;\n white-space: nowrap;\n }\n #customBtn:hover {\n cursor: pointer;\n }\n span.label {\n font-family: serif;\n font-weight: normal;\n }\n span.icon {\n background: url('/identity/sign-in/g-normal.png') transparent 5px 50% no-repeat;\n display: inline-block;\n vertical-align: middle;\n width: 42px;\n height: 42px;\n }\n span.buttonText {\n display: inline-block;\n vertical-align: middle;\n padding-left: 42px;\n padding-right: 42px;\n font-size: 14px;\n font-weight: bold;\n /* Use the Roboto font that is loaded in the \u003chead\u003e */\n font-family: 'Roboto', sans-serif;\n }\n \u003c/style\u003e\n \u003c/head\u003e\n \u003cbody\u003e\n \u003c!-- In the callback, you would hide the gSignInWrapper element on a\n successful sign in --\u003e\n \u003cdiv id=\"gSignInWrapper\"\u003e\n \u003cspan class=\"label\"\u003eSign in with:\u003c/span\u003e\n \u003cdiv id=\"customBtn\" class=\"customGPlusSignIn\"\u003e\n \u003cspan class=\"icon\"\u003e\u003c/span\u003e\n \u003cspan class=\"buttonText\"\u003eGoogle\u003c/span\u003e\n \u003c/div\u003e\n \u003c/div\u003e\n \u003cdiv id=\"name\"\u003e\u003c/div\u003e\n \u003cscript\u003estartApp();\u003c/script\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n```"]]