google-apps-script - 每台计算机上同一 Web 应用程序的不同 JavaScript 来源导致错误 : redirect_uri_mismatch

标签 google-apps-script iframe web-applications google-signin

我已经使用 Google 应用程序脚本发布了一个带有 Google 登录的 WebApp。每次您在另一台计算机上登录您的谷歌帐户访问此网络应用程序时,都会出现错误:“redirect_uri_mismatch”。

我看到在每台登录到 google 帐户的计算机上,已发布的网络应用程序在请求链接中具有不同的 javaScript 源,我已将此链接添加到 Google 开发人员控制台上的授权 JavaScript 源中,以便这台计算机登录以及使 Google签到工作。

我希望我的 console.developers.google.com 只有一个授权的 JavaScript 来源(链接应用程序脚本)。我希望所有用户访问和登录时不会出现错误“redirect_uri_mismatch”

上千用户不方便,如何改进?

HTML 代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="google-signin-client_id" content="1xxxxxxxxxx-xxxxxxxxi87eht.apps.googleusercontent.com">
    <title>Oauth2 web</title>

    <!-- Google library -->
    <script src="https://apis.google.com/js/platform.js" async defer></script>

    <!-- Jquery library to print the information easier -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>

    <!-- Bootstrap library for the button style-->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<div id="profileinfo">
</div>
<div class="g-signin2" data-onsuccess="onSignIn"></div>

<script>
            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()); // This is null if the 'email' scope is not present.

              $("#profileinfo").append("<h2>Sup " + profile.getName() + ", welcome home my friend</h2>");
              $("#profileinfo").append("<img style='width:250px;height:250px' src='" + profile.getImageUrl() + "'><br><br>");
              $("#profileinfo").append("<p>Your email is: " + profile.getEmail() + "</p>");

            }
        </script>

<button type="button" class="btn btn-danger" onclick="signOut();">Sign out</button>

<script>
            function signOut() {
               var auth2 = gapi.auth2.getAuthInstance();
               auth2.signOut().then(function () {
                 console.log('User signed out.');
               $("#profileinfo").empty();
               $("#profileinfo").append("<h2>Goodbye old friend</h2>");
               });
            }
        </script>
</body>
</html>

在另一台电脑上登录时出错: Error when logging in on another computer:

链接的变化以黄色突出显示: The change of the link is highlighted in yellow

最佳答案

这可能行不通,但您可以使用 index.html 中的脚本将默认值设置为 0:

<script>
  if(!/-0lu/.test(location.href)){ 
    location.href = location.href.toString().replace(/-\d+lu/,'-0lu');
  }
</script>

您的网络应用 script.google.com 有一个沙盒 iframe *[DIGIT]lu-script.googleusercontent.com。我们只是尝试使用正则表达式更改此 iframe 的位置。如果不是 0lu 作为 iframe 存在,我们将其替换为 0lu

关于google-apps-script - 每台计算机上同一 Web 应用程序的不同 JavaScript 来源导致错误 : redirect_uri_mismatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56862586/

相关文章:

google-apps-script - 如何在 Google API 控制台中添加 Google Apps 脚本网络应用作为客户端

javascript - 以编程方式更改 Google 应用程序脚本的时区

user-interface - 在 Google Apps 脚本 Google Sheets Workspace 插件中使用 CardService 时出错

javascript - 使用 YouTube API 嵌入视频会导致 404 错误

python - Django Apps 应该如何捆绑静态媒体?

google-apps-script - 复制工作表时,谷歌应用程序验证从谷歌应用程序脚本项目中丢失

javascript - 如何在 iFrame 中模拟按键

javascript - 如何为我的网页提供 HTML 嵌入代码?

google-apps-script - 我是否正确使用了 Google Apps 脚本锁定服务?

javascript - 保护网络应用程序免受信息泄露