Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在ReactJS中实现简单的googleoauth_Javascript_Node.js_Reactjs_Oauth 2.0_Google Oauth - Fatal编程技术网

Javascript 在ReactJS中实现简单的googleoauth

Javascript 在ReactJS中实现简单的googleoauth,javascript,node.js,reactjs,oauth-2.0,google-oauth,Javascript,Node.js,Reactjs,Oauth 2.0,Google Oauth,我有一个迷你香草JS应用程序,设置如下: <!DOCTYPE html> <html lang="en"> <head> <script src="https://apis.google.com/js/platform.js" async defer></script> <meta name="google-signin-client_id"

我有一个迷你香草JS应用程序,设置如下:

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://apis.google.com/js/platform.js" async defer></script>
    <meta
      name="google-signin-client_id"
      content="{{client_id}}" /> <!--Here goes my actual WORKING google client id-->
    />
  </head>
  <body>
    <div class="g-signin2" data-onsuccess="onSignIn"></div>
 
    <button onclick="signOut()">Sign Out</button>
    <script>
      function onSignIn(googleUser) {
        const 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()); // This is null if the 'email' scope is not present.
        }

      function signOut() {
        var auth2 = gapi.auth2.getAuthInstance();
        auth2.signOut().then(function () {
          console.log("User signed out.");
        });
      }

    </script>
  </body>
</html>


/>
退出
函数onSignIn(谷歌用户){
const profile=googleUser.getBasicProfile();
console.log(“ID:+profile.getId());//不要发送到后端!改用ID令牌。
log(“Name:+profile.getName());
log(“图像URL:+profile.getImageUrl());
console.log(“Email:+profile.getEmail());//如果“Email”作用域不存在,则为null。
}
函数签出(){
var auth2=gapi.auth2.getAuthInstance();
auth2.signOut().then(函数(){
log(“用户注销”);
});
}
在这里,我登录一个用户,然后我可以使用谷歌将他们注销。 当我试图将其转移到React项目时,我遇到了一些问题:

  • 无法执行VanillaJS应用程序中的函数(引号之间)
  • 即使我在React项目中的
    public/index.html
    中包含脚本标记,我也没有 可以访问
    gapi
    变量。 有人知道怎么做吗

    • 我想你可以使用这个库。 或者你们也可以阅读那个份回购协议的github,看看如何抓取GoogleOAuth来应对这个应用程序