最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why decodeJwtResponse is not defined at Nn.handleCredentialResponse [as callback] in php project? - Stack Overflow

programmeradmin0浏览0评论
<body>
   <script src="; async defer></script>
   <div id="g_id_onload"
     data-client_id="790854323959-v2lniefhl7ripoijm0ooetu9ari91g3q.apps.googleusercontent"
     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>

Note: I want to integrate google login in php project using javascript. But I am getting error like- decodeJwtResponse is not defined at Nn.handleCredentialResponse [as callback]. Please help me, I want to get profile details. please provide me all the details regarding my query.

<body>
   <script src="https://accounts.google./gsi/client" async defer></script>
   <div id="g_id_onload"
     data-client_id="790854323959-v2lniefhl7ripoijm0ooetu9ari91g3q.apps.googleusercontent."
     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>

Note: I want to integrate google login in php project using javascript. But I am getting error like- decodeJwtResponse is not defined at Nn.handleCredentialResponse [as callback]. Please help me, I want to get profile details. please provide me all the details regarding my query.

Share Improve this question edited Aug 2, 2022 at 17:16 asked Aug 2, 2022 at 16:50 user19165954user19165954
Add a ment  | 

2 Answers 2

Reset to default 10

This will work with a valid response

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);
  }

The answer is right there in the code you copied from Google.

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

You need to define the function yourself to decode the CredentialResponse.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论