nuxt auth :Google 提供商返回 invalid_request

问题描述

我尝试使用 Nuxt Auth 模块设置 google 身份验证,但从 google 收到以下错误

Error 400 : invalid_request
Parameter not allowed for this message type: nonce

这是我的配置

// nuxt.config.js
modules: ["@nuxtjs/axios","@nuxtjs/auth-next"],auth: {
  router: {
    middleware: ["auth"],},redirect: {
    login: "/login",logout: "/",callback: false,home: "/",strategies: {
         google: { clientId: "MyClientID",codeChallengeMethod: "" }
  }
}

以及我如何在我的组件中调用 google auth:

login(){
   this.$auth.loginWith("google").then( result => console.log(result) )
}

我也尝试从这里运行官方演示: https://github.com/nuxt-community/auth-module/tree/dev/demo 但我遇到了同样的错误

解决方法

有同样的问题,但设置 responseType: 'code' 对我有用。

编辑:为隐式授权流程设置 responseType: "id_token token" 并使用 Google 的访问令牌重定向到您的 Nuxt 应用。整个 OAuth 主题对我来说一直很困惑,并且在安全方面有很多关于该做什么和不该做什么的错误信息。我发现以下文章非常有帮助,并揭开了各种 OAuth 流程的神秘面纱:https://itnext.io/an-oauth-2-0-introduction-for-beginners-6e386b19f7a9 但是,如果您不想在前端公开访问令牌而是在后端获取它,那么您必须使用代码授权流程,通过设置 responseType: "code" 并正确设置您的后端。我最终使用了代码授权流程,但我认为大多数人发现隐式授权流程更方便。

这是完整的片段:

export default {
  modules: ["@nuxtjs/axios","@nuxtjs/auth-next"],auth: {
    strategies: {
      google: {
        clientId: process.env.GOOGLE_CLIENT_ID,redirectUri: `${process.env.BASE_URL}/auth/google/callback`,codeChallengeMethod: "",responseType: "id_token token",},router: {
    middleware: ["auth"],};
,

好像跟Nuxt Auth的版本有关。

也许这个功能在 @nuxtjs/auth-next 中没有准备好

所以我跑

npm install @nuxtjs/auth@latest --save

然后更新nuxt.config.json

  1. @nuxtjs/auth-next替换@nuxtjs/auth
  2. clientId替换client_id
// nuxt.config.js
modules: ["@nuxtjs/axios","@nuxtjs/auth"],auth: {
  router: {
    middleware: ["auth"],redirect: {
    login: "/login",logout: "/",callback: false,home: "/",strategies: {
         google: { client_id: "MyClientID"}
  }
}
,

在 auth-next 和 auth0 中,responseType 的设置让我绕过了这个问题,我的工作配置如下:

auth0: {
  logoutRedirectUri: process.env.BASE_URL,domain: String(process.env.AUTH0_DOMAIN).replace(/(^\w+:|^)\/\//,''),clientId: process.env.AUTH0_CLIENT_ID,scope: ['read:reports','read:roles','create:client_grants','offline_access'],// 'openid','profile','email' default added
  audience: process.env.AUTH0_AUDIENCE,responseType: 'token',accessType: 'offline',grantType: 'client_credentials',redirectUri: process.env.BASE_URL + '/callback',codeChallengeMethod: 'S256'
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...