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

0 javascript php google-authentication

<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>
Run Code Online (Sandbox Code Playgroud)

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

Fer*_*gus 8

这将与有效的响应一起使用

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);
  }
Run Code Online (Sandbox Code Playgroud)

  • 这个答案对我有用! (2认同)