我在本说明中遵循此流程: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);
}
但目前我必须处理重定向网址以与 console.google 项目中的配置匹配。
我认为在这种情况下检查重定向网址是不必要的。
那么如何禁用检查重定向网址或任何想法?
最佳答案
重定向 uri 验证检查是 Google 授权流程的一部分。您无法在 Web 项目上禁用它。授权服务器需要知道将您的授权码返回到哪里。如果您正在运行 Web 应用程序,那么您始终需要定义重定向 uri。
另一方面,如果您正在运行服务器端应用程序或已安装的应用程序,那么您不应该使用网络浏览器客户端,而应该使用不会使用重定向 uri 的 native 客户端。
此示例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);
});
}
关于node.js - 如何禁用 googleapi 上的检查重定向 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55589873/