JavaScript API'yi kullanma

Dilerseniz tek dokunuş istemini tetiklemek veya Google ile oturum aç düğmesini oluşturmak için yalnızca JavaScript kodu kullanabilirsiniz.

JavaScript'te Tek Dokunma istemini yapılandırmak için önce initialize() yöntemini çağırmanız gerekir. Ardından, istem kullanıcı arayüzünü görüntülemek için prompt() yöntemini çağırın. Aşağıdaki kod snippet'ine bakın:

<script>
  window
.onload = function () {
    google
.accounts.id.initialize({
      client_id
: 'YOUR_GOOGLE_CLIENT_ID',
      callback
: handleCredentialResponse
   
});
    google
.accounts.id.prompt();
 
}
</script>

İstem kullanıcı arayüzü durum bildirimlerini almak için prompt() yöntemine bir geri çağırma işlevi sağlayın. Aşağıdaki kod snippet'ine bakın:

<script>
  window
.onload = function () {
    google
.accounts.id.initialize({
      client_id
: 'YOUR_GOOGLE_CLIENT_ID',
      callback
: handleCredentialResponse
   
});
    google
.accounts.id.prompt((notification) => {
       
if (notification.isNotDisplayed() || notification.isSkippedMoment()) {
           
// try next provider if OneTap is not displayed or skipped
       
}
   
});
 
}
</script>

Aşağıdaki örnek kodda, hem Tek Dokunuş hem de Google ile oturum açma düğmesinin JavaScript'te nasıl oluşturulacağı gösterilmektedir.

<script>
  window
.onload = function () {
    google
.accounts.id.initialize({
      client_id
: 'YOUR_GOOGLE_CLIENT_ID',
      callback
: handleCredentialResponse
   
});
   
const parent = document.getElementById('google_btn');
    google
.accounts.id.renderButton(parent, {theme: "filled_blue"});
    google
.accounts.id.prompt();
 
}
</script>