【发布时间】:2017-01-03 16:30:28
【问题描述】:
我不会在没有搜索和阅读文档的情况下提出这个问题。到目前为止我花了2天。我确定我错过了某事。 我正在尝试在驱动器电子表格上实现谷歌身份验证。我已经尝试了所有方法,但仍然收到错误消息(redirect_uri_mismatch)。基本上,我想要一个带有登录屏幕的侧面板。用户单击按钮,auth magic 运行并在用户允许访问时重定向到另一个 html 打印“成功”。
- 我在 google 开发控制台中创建了一个项目。
-
创建的凭据
2.1 客户编号:
131579675294-jc1c0ckuaa7n7ih7eevg19cisthgt00e.apps.googleusercontent.com2.2 客户端密码:XaebFsC18qfMmcZJKgokLEYo
设置回调 uri:
https://script.google.com/macros/d/MCgMJPIdD1bbeG1PsFaNug8uUifae5TWT/usercallback-
项目密钥: MCgMJPIdD1bbeG1PsFaNug8uUifae5TWT
脚本 ID: 1DYEShH45-AtikbEwfAG8w9P7Y39FHhCB-nGHWHOW4mUtq5DZLvubDxev
据说 projectKey 已被弃用,而是需要使用脚本 ID,但两者都不起作用。
我使用 oauth2,所以我添加了 the external lib : 1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF
说明:我的 gs 文件有以下代码。单击时,我有一个带有按钮调用 onSignIn() 的侧边栏。我期待使用身份验证访问电子表格。作为起点,我想查看授权页面。接受它后,我希望它重定向到 callback_uri 页面并显示一些简单的内容。但是它确实给了我错误。具有讽刺意味的是,我创建的端点浏览器链接工作并成功重定向。
端点浏览器链接
我做错了什么?感谢您的帮助。谢谢。
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.
【问题讨论】:
-
@ShyamKansagra 非常感谢!我非常确定我使用了正确的客户 ID。通过 Resources -> Advanced services 访问后,我意识到它是不对的。更换了正确的,但仍然没有运行。然后我创建了一个新表并将所有代码带到新表中。然后它起作用了!我不知道如何相信你的评论。告诉我。
-
不客气 :),我添加了相同的评论作为答案。你可以接受。
标签: google-apps-script google-sheets oauth2