谷歌脚本oauth2错误:redirect_uri_mismatch



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

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

    2.2客户秘密:XaebFsC18qfMmcZJKgokLEYo

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

  4. 项目密钥:MCgMJPIdD1bbeG1PsFaNug8uUifae5TWT

    脚本id:1DYEShH45-TikbEwfAG8w9P7Y39FHhCB-nGHWHOW4mUtq5DZLvubDxev

    假定projectKey已弃用,需要使用脚本id,但两者都不起作用。

  5. 我使用oauth2,所以我添加了外部库: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=代码&client_id=131579675294-jc1c0ckuaa7n7ih7eevg19cischgt00e.apps.googleusercontent.com&approvel_prompt=强制&scope=https%3A%2F%2Fdocs.google.com%2Ffeeds

我做错了什么?非常感谢你的帮助。Thx。

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.
  1. 转到资源->高级服务。单击底部的谷歌开发者控制台。

  2. 您会打开API管理器。

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

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

单击"保存",然后重试。

最新更新