Google 脚本 oauth2 错误:redirect_uri_mismatch

Bin*_*yWo 1 google-sheets google-apps-script oauth2

我不会在没有搜索和阅读文档的情况下问这个问题。到目前为止我花了2天。我确定我错过了什么。我正在尝试在驱动器电子表格上实施谷歌身份验证。我已经尝试了所有方法,但仍然收到错误消息(redirect_uri_mismatch)。基本上,我想要一个带有登录屏幕的侧面板。当用户允许访问时,用户单击按钮,auth magic 运行并重定向到另一个打印“成功”的 html。

  1. 我在谷歌开发控制台中创建了一个项目。
  2. 创建凭据
    2.1 客户端 ID:
    131579675294-jc1c0ckuaa7n7ih7eevg19cisthgt00e.apps.googleusercontent.com

    2.2 客户端密码:XaebFsC18qfMmcZJKgokLEYo

  3. 设置回调uri:
    https://script.google.com/macros/d/MCgMJPIdD1bbeG1PsFaNug8uUifae5TWT/usercallback

  4. 项目密钥: MCgMJPIdD1bbeG1PsFaNug8uUifae5TWT

    脚本 ID: 1DYEShH45-AtikbEwfAG8w9P7Y39FHhCB-nGHWHOW4mUtq5DZLvubDxev

    据说 projectKey 已被弃用,而是需要使用脚本 ID,但两者都不起作用。

  5. 我使用 oauth2 所以我添加了外部库:1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF

  6. 说明:我的 gs 文件有以下代码。我有一个侧边栏,上面有一个按钮,单击时会调用 onSignIn()。我希望通过身份验证访问电子表格。首先,我想查看授权页面。接受后,我希望它重定向到一个页面,即 callback_uri 并显示一些简单的内容。但是它确实给了我错误。具有讽刺意味的是,我创建的端点浏览器链接工作并成功重定向。

端点浏览器链接

https://accounts.google.com/o/oauth2/auth?redirect_uri=https%3A%2F%2Fscript.google.com%2Fmacros%2Fd%2FMCgMJPIdD1bbeG1PsFaNug8uUifae5TWT%2Fusercallback&response_type=code&client_id=131579675294-jc1c0ckuaa7n7ih7eevg19cisthgt00e.apps.googleusercontent.com&approval_prompt= force&scope=https%3A%2F%2Fdocs.google.com%2Ffeeds

我究竟做错了什么?感谢您的帮助。谢谢。

    var CLIENT_ID = '131579675294-jc1c0ckuaa7n7ih7eevg19cisthgt00e.apps.googleusercontent.com';

    var CLIENT_SECRET = 'XaebFsC18qfMmcZJKgokLEYo';

    function onSignIn() {
        var service = getService();
        if (!service.hasAccess()) {
          var authorizationUrl = service.getAuthorizationUrl();
          var template = HtmlService.createTemplate('<a href="<?= authorizationUrl ?>" target="_blank">Authorize</a>');
          template.authorizationUrl = authorizationUrl;
          var page = template.evaluate();
          return HtmlService.createHtmlOutput( page);
        }
    }


      function authCallback(request) {
        var service = getService();
        var authorized = service.handleCallback(request);
        if (authorized) {
            return HtmlService.createHtmlOutput('Success!');
        } else {
            return HtmlService.createHtmlOutput('Denied');
        }
      }



      function getService() {
        return OAuth2.createService('spreadsheets_ozzy123')

            .setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')

            .setTokenUrl('https://accounts.google.com/o/oauth2/token')

            .setClientId(CLIENT_ID)

            .setClientSecret(CLIENT_SECRET)

            .setCallbackFunction('authCallback')

            .setScope('https://docs.google.com/feeds')  ;   
      }




      function onOpen() {
            SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
                .createMenu('Custom Menu')
                .addItem('Show sidebar', 'showSidebar')
                .addToUi();  
      }


      function showSidebar() {
         var html =  HtmlService.createTemplateFromFile('LoginSideMenu').evaluate();
            SpreadsheetApp.getUi().showSidebar(html); 
      }


      function include(filename) {
          return HtmlService.createHtmlOutputFromFile(filename).getContent();
      }
Run Code Online (Sandbox Code Playgroud)

完全错误

400. That’s an error.

Error: redirect_uri_mismatch

The JavaScript origin in the request, https://n-g7vwwdjiqopmv3hpcys4noea4krn6nxax6uaoda-0lu-script.googleusercontent.com, does not match the ones authorized for the OAuth client. Visit https://console.developers.google.com/apis/credentials/oauthclient/131579675294-jc1c0ckuaa7n7ih7eevg19cisthgt00e.apps.googleusercontent.com?project=131579675294 to update the authorized JavaScript origins.

Learn more

Request Details
redirect_uri=storagerelay://https/n-g7vwwdjiqopmv3hpcys4noea4krn6nxax6uaoda-0lu-script.googleusercontent.com?id=auth704130
response_type=permission id_token
scope=email profile openid
openid.realm=
client_id=131579675294-jc1c0ckuaa7n7ih7eevg19cisthgt00e.apps.googleusercontent.com
ss_domain=https://n-g7vwwdjiqopmv3hpcys4noea4krn6nxax6uaoda-0lu-script.googleusercontent.com
fetch_basic_profile=true
gsiwebsdk=2
That’s all we know.
Run Code Online (Sandbox Code Playgroud)

Shy*_*gra 5

  1. 转至资源 -> 高级服务。点击底部的 Google 开发者控制台。

  2. 你会打开 API 管理器。

  3. 现在,转到最左侧面板中的凭据。

  4. 确保您使用了与代码中显示的相同的客户端 ID。此外,还有 2 个选项:授权的 js 来源和授权的重定向 url。
  5. 在 url 选项中,粘贴 400 错误中不匹配的 url。

单击保存并重试。