首页 > 解决方案 > 每台计算机上同一 Web 应用程序的不同 JavaScript 源导致错误:redirect_uri_mismatch

问题描述

我使用 Google 应用程序脚本发布了一个带有 Google 登录的 WebApp。每次您在另一台计算机上登录您的 google 帐户以访问此网络应用程序时,都会出现错误:“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>

在另一台计算机上登录时出错: 在另一台计算机上登录时出错:

链接的更改以黄色突出显示: 链接的更改以黄色突出显示

标签: google-apps-scriptiframeweb-applicationsgoogle-signin

解决方案


这可能不起作用,但您可以在 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 的位置。如果 iframe 以外0lu的其他内容,我们将其替换为0lu


推荐阅读