【问题标题】:How to disable check redirect url on googleapi?如何在googleapi上禁用检查重定向网址?
【发布时间】:2019-08-30 13:46:00
【问题描述】:
我在此说明中遵循此流程:https://developers.google.com/identity/sign-in/web/server-side-flow 以通过 google 进行身份验证。
在后端服务器中,我使用 lib : google-auth-lib
const { OAuth2Client } = require('google-auth-library');
async function login (code) {
const auth = new OAuth2Client(
googleConfig.clientId,
googleConfig.clientSecret,
googleConfig.redirect
);
const data = await auth.getToken(code);
}
但目前我必须处理重定向 url 以匹配 console.google 项目中的配置。
我认为这种情况不需要检查重定向网址。
那么我该如何禁用检查重定向网址或任何想法?
【问题讨论】:
标签:
node.js
oauth
google-api
google-oauth
google-api-nodejs-client
【解决方案1】:
重定向 uri 验证检查是 Google 授权过程的一部分。您不能在 Web 项目中禁用它。授权服务器需要知道将您的授权码返回到哪里。如果您正在运行一个 Web 应用程序,那么您将始终需要定义一个重定向 uri。
另一方面,如果您正在运行服务器端应用程序或已安装的应用程序,那么您不应该使用 Web 浏览器客户端,而应该使用不会使用重定向 uri 的本机客户端。
此示例node quickstart 旨在作为控制台应用程序运行以访问 google drive api。它可能会帮助你。
/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
function authorize(credentials, callback) {
const {client_secret, client_id, redirect_uris} = credentials.installed;
const oAuth2Client = new google.auth.OAuth2(
client_id, client_secret, redirect_uris[0]);
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, (err, token) => {
if (err) return getAccessToken(oAuth2Client, callback);
oAuth2Client.setCredentials(JSON.parse(token));
callback(oAuth2Client);
});
}
【讨论】:
-
我试过了,但redirect_uris[0] 与frontEnd 上的redirect_url 不匹配?我为某些环境配置了一些 redirect_uris。 Exp: FrontEnd setup redirect_url = a.com 并从 google 请求代码,将此代码发送到 BackEnd,BackEnd 获取 Uris 的默认第一个参数,但它与 a.com 不匹配