为什么decodeJwtResponse没有在php项目中的Nn.handleCredentialResponse [as callback]中定义?

gkl3eglg  于 2023-01-19  发布在  PHP
关注(0)|答案(2)|浏览(94)
<body>
   <script src="https://accounts.google.com/gsi/client" async defer></script>
   <div id="g_id_onload"
     data-client_id="790854323959-v2lniefhl7ripoijm0ooetu9ari91g3q.apps.googleusercontent.com"
     data-callback="handleCredentialResponse">
   </div>
   <div class="g_id_signin" data-type="standard"></div>
  
   <script>
      function handleCredentialResponse(response) {
          // decodeJwtResponse() is a custom function defined by you
         // to decode the credential response.
         const responsePayload = decodeJwtResponse(response.credential);
         console.log("ID: " + responsePayload.sub);
         console.log('Full Name: ' + responsePayload.name);
         console.log('Given Name: ' + responsePayload.given_name);
         console.log('Family Name: ' + responsePayload.family_name);
         console.log("Image URL: " + responsePayload.picture);
         console.log("Email: " + responsePayload.email);
      }
   </script>
</body>

注意:我想使用javascript将google登录整合到php项目中。但是我收到了错误信息,比如- decodeJwtResponse没有在Nn.handleCredentialResponse [作为回调]中定义。请帮助我,我想获得个人资料的详细信息。请提供我所有关于我查询的详细信息。

vxf3dgd4

vxf3dgd41#

答案就在你从谷歌复制的代码里。

// decodeJwtResponse() is a custom function defined by you
// to decode the credential response.

您需要自己定义函数来解码CredentialResponse。

2hh7jdfx

2hh7jdfx2#

这将与有效响应一起工作

function decodeJwtResponse(token) {
    var base64Url = token.split(".")[1];
    var base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
    var jsonPayload = decodeURIComponent(
      atob(base64)
        .split("")
        .map(function (c) {
          return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
        })
        .join("")
    );

    return JSON.parse(jsonPayload);
  }

相关问题