2017-01-03 173 views
1

我不问这个问题没有搜索和阅读文档。我到目前为止花了2天时间。我相信我失踪了。 我正试图在驱动器电子表格上实现谷歌身份验证。我尝试了一切,但仍然收到错误消息(redirect_uri_mismatch)。基本上,我想要一个带有登录屏幕的侧面板。用户点击按钮,授权魔法运行,并在用户允许访问时重定向到另一个HTML打印“成功”。谷歌脚本oauth2错误:redirect_uri_mismatch

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

    2.2客户端的秘密:XaebFsC18qfMmcZJKgokLEYo

  3. 集回调URI:
    https://开头script.google .com/macros/d/MCgMJPIdD1bbeG1PsFaNug8uUifae5TWT/usercallback

  4. project key: MCgMJPIdD1bbeG1PsFaNug8uUifae5TWT

    脚本ID: 1DYEShH45-AtikbEwfAG8w9P7Y39FHhCB-nGHWHOW4mUtq5DZLvubDxev

    据称projectKey已被弃用,取而代之的脚本ID需要使用但既不工程。

  5. 我用的oauth2所以我加了the external lib:1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF

  6. 说明:我的GS文件有下面的下面的代码。点击时,我有一个带按钮的边栏调用onSignIn()。我期望通过auth访问电子表格。作为一个起点,我想看看授权页面。接受它之后,我希望它重定向到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(); 
     } 

完整的错误

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. 
+0

@ShyamKansagra非常感谢!我很确定我使用的是正确的客户端ID。通过资源 - >高级服务访问后,我意识到这是不对的。替换正确的,但仍然没有运行。然后我创建了一个新工作表并将所有代码传送到新工作表。然后它的工作!我不知道如何评论你的评论。让我知道。 – BinaryWo

+0

不客气:),我已经添加了相同的评论作为答案。你可以接受它。 –

回答

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

  2. 您将会打开API管理器。

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

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

点击保存并重试。

相关问题