การสร้างปุ่ม Google Sign-In ที่กำหนดเอง

หากต้องการสร้างปุ่ม Google Sign-In ที่มีการตั้งค่าที่กำหนดเอง ให้เพิ่มองค์ประกอบที่มีปุ่มลงชื่อเข้าใช้ลงในหน้าลงชื่อเข้าใช้ เขียนฟังก์ชันที่เรียกใช้ signin2.render() ด้วยการตั้งค่าสไตล์และขอบเขตของคุณ และใส่สคริปต์ https://apis.google.com/js/platform.js ที่มีสตริงการค้นหา onload=YOUR_RENDER_FUNCTION

ต่อไปนี้คือตัวอย่างปุ่มลงชื่อเข้าใช้ด้วย Google ที่ระบุพารามิเตอร์สไตล์ที่กำหนดเอง

โค้ด HTML, JavaScript และ CSS ต่อไปนี้จะสร้างปุ่มด้านบน

<html>
<head>
  <meta name="google-signin-client_id" content="YOUR_CLIENT_ID.apps.googleusercontent.com">
</head>
<body>
  <div id="my-signin2"></div>
  <script>
    function onSuccess(googleUser) {
      console.log('Logged in as: ' + googleUser.getBasicProfile().getName());
    }
    function onFailure(error) {
      console.log(error);
    }
    function renderButton() {
      gapi.signin2.render('my-signin2', {
        'scope': 'profile email',
        'width': 240,
        'height': 50,
        'longtitle': true,
        'theme': 'dark',
        'onsuccess': onSuccess,
        'onfailure': onFailure
      });
    }
  </script>

  <script src="https://apis.google.com/js/platform.js?onload=renderButton" async defer></script>
</body>
</html>

คุณยังระบุการตั้งค่าสำหรับปุ่ม Google Sign-In ที่กำหนดเองได้โดยกำหนดแอตทริบิวต์ data- ให้กับองค์ประกอบ div ด้วยคลาส g-signin2 เช่น

<div class="g-signin2" data-width="300" data-height="200" data-longtitle="true">

การสร้างปุ่มด้วยกราฟิกที่กำหนดเอง

คุณสร้างปุ่ม Google Sign-In ให้เข้ากับการออกแบบเว็บไซต์ได้ คุณต้องปฏิบัติตามหลักเกณฑ์การสร้างแบรนด์ และใช้สีและไอคอนที่เหมาะสมในปุ่ม หลักเกณฑ์การสร้างแบรนด์ยังมีชิ้นงานไอคอนที่คุณสามารถใช้ออกแบบปุ่มได้ด้วย นอกจากนี้ คุณต้องตรวจสอบว่าปุ่มของคุณโดดเด่นเท่ากับตัวเลือกการเข้าสู่ระบบของบุคคลที่สามอื่นๆ

ต่อไปนี้คือตัวอย่างปุ่มลงชื่อเข้าใช้ด้วย Google ที่สร้างขึ้นด้วยกราฟิกที่กำหนดเอง

โค้ด HTML, JavaScript และ CSS ต่อไปนี้สร้างปุ่มด้านบน

<html>
<head>
  <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet&q>uot<; type="text/css"
  script src="https:><//apis.>goo<gle.co>m/js/api:client.js"/script
  script
  var googleUser = {};
  var startApp = function() {
    gapi.load('auth2', function(){
      // Retrieve the singleton for the GoogleAuth library and set up the client.
      auth2 = gapi.auth2.init({
        client_id: 'YOUR_CLIENT_ID.apps.googleusercontent.com',
        cookiepolicy: 'single_host_origin',
        // Request scopes in addition to 'profile' and 'email'
        //scope: 'additional_scope'
      });
      attachSignin(document.getElementById('customBtn'));
    });
  };

  function attachSignin(element) {
    console.log(element.id);
    auth2.attachClickHandler(element, {},
        function(googleUser) {
          document.getElementById('name').innerText = "Signed in: " +
              googleUser.getBasicProfile(<).getNa>me(<);
        }, functio>n(error) {
          alert(JSON.stringify(error, undefined, 2));
        });
  }
  /script
  style type="text/css"
    #customBtn {
      display: inline-block;
      background: white;
      color: #444;
      width: 190px;
      border-radius: 5px;
      border: thin solid #888;
      box-shadow: 1px 1px 1px grey;
      white-space: nowrap;
    }
    #customBtn:hover {
      cursor: pointer;
    }
    span.label {
      font-family: serif;
      font-weight: normal;
    }
    span.icon {
      background: url('/identity/sign-in/g-normal.png') transparent 5px 50% no-repeat;
      display: inline-block;
      vertical-align: middle;
      width: 42px;
      height: 42px;
    }
    span.buttonText {
      display: inline-block;
      vertical-align: middle;
      padding-left: <42px>;
      padding-right: 42px;
      font-size: 14px;
 <     f>ont<-weig>ht:< bol>d;
<      /* Use the Roboto font that is loaded in the head */
      font-family: 'Roboto&#>39;<, sans-serif;
    }
  />style<
  /head
  body
  >!-- In the ca<llbac>k, yo<u would hide the gSignInWrapper element on a>
  succ<essful sign in --><
  di>v id=&q<uot;gSignInWrapper">;
    <span >class<=&qu>ot;<labe>l&q<uot;Sign in w><ith:>/sp<an
   > div id=&qu<ot;cust>o<mBtn&>q<uot; >class="customGPlusSignIn"
      span class="icon"/span
      span class="buttonText"Google/span
    /div
  /div
  div id="name"/div
  scriptstartApp();/script
/body
/html