为Google登录调用data-onsuccess方法

问题描述 投票:9回答:3

我正在尝试将Google Sign In集成到我的网络应用程序中。

我的页面有Google按钮:

ModalDialogue.cshtml:

<a class="google g-signin2" data-onsuccess="onSignIn">Sign up with Google+</a>

我试图在我的js中触发该方法:

ModalDialogue.js:

      function onSignIn(googleUser) {
        var profile = googleUser.getBasicProfile();
        console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
        console.log('Name: ' + profile.getName());
        console.log('Image URL: ' + profile.getImageUrl());
        console.log('Email: ' + profile.getEmail());
      }

我知道

a] JavaScript在页面范围内,因为我的其他功能 - 用于打开和关闭对话 - 可以工作。

b]该按钮正在与Google通信,因为单击该按钮会打开“使用Google登录”对话框 - 然后完成后 - 将按钮更改为“登录”。

如何触发onSignIn方法?或者至少如何确定是否触发了成功?

javascript google-signin
3个回答
3
投票

哦。我的错。我没有注意到我把方法包裹在一个闭包中。需要全球化。


3
投票

对于将来访问此页面的人,我不得不将功能定义移到按钮标记上方。看起来它应该无关紧要,因为data-onsuccess只是一个字符串。遗憾的是,它没有抛出任何错误消息。


0
投票
<html lang="en">
  <head>
    <meta name="google-signin-scope" content="profile email">
    <meta name="google-signin-client_id" content="YOUR_CLIENT_ID.apps.googleusercontent.com">
    <script src="https://apis.google.com/js/platform.js" async defer></script>
  </head>
  <body>
    <div class="g-signin2" data-onsuccess="onSignIn" data-theme="dark"></div>
    <script>
      function onSignIn(googleUser) {
        // Useful data for your client-side scripts:
        var profile = googleUser.getBasicProfile();
        console.log("ID: " + profile.getId()); // Don't send this directly to your server!
        console.log('Full Name: ' + profile.getName());
        console.log('Given Name: ' + profile.getGivenName());
        console.log('Family Name: ' + profile.getFamilyName());
        console.log("Image URL: " + profile.getImageUrl());
        console.log("Email: " + profile.getEmail());

        // The ID token you need to pass to your backend:
        var id_token = googleUser.getAuthResponse().id_token;
        console.log("ID Token: " + id_token);
      }
    </script>
  </body>
</html>

for for infofor info

© www.soinside.com 2019 - 2024. All rights reserved.