使用 strapi-google-auth npm 包在 strapi 中进行 Google 身份验证

问题描述 投票:0回答:1

作为序言,我正在使用 strapi v4、react 和 astro 来构建我的应用程序,给我带来麻烦的插件是 strapi-google-auth。我正在尝试在我的 strapi 应用程序中实施谷歌身份验证,但我似乎无法弄清楚我收到的请求错误。我一直按照这些步骤进行操作,直到我必须请求 JWT 令牌。所以用户可以使用google登录,但是我因为400 bad request错误而无法获取JWT。

我的前端代码:

const login = async () => {
      let authCode = queryString.parse(window.location.search)
      console.log(authCode.code)
      let googleAuth = {
        data: {
          code: authCode.code,
        }
      }
      let jwt = await axios.post('http://127.0.0.1:1338/strapi-google-auth/user-profile', googleAuth)
        .then((res) => setJwt(res.data))
      
    }

错误:

{"data":null,"error":{"status":400,"name":"BadRequestError","message":"Error occured while fetching the user profile","details":null}}

The problem step

我做错了什么吗?

我试过摆弄有效负载的格式化方式,卸载并重新安装插件,并通过邮递员运行请求。

axios google-oauth strapi
1个回答
0
投票

尝试将

googleAuth
对象值更改为:

// Some code
// ...

let googleAuth = {
  code: authCode.code,
}

let jwt = await axios.post(
  'http://127.0.0.1:1338/strapi-google-auth/user-profile',
  googleAuth
).then((res) => setJwt(res.data))

axios post 的第二个参数是

data
,然后你可以在那里传递对象。

axios.post('STRAPI_BACKEND_URL/strapi-google-auth/user-profile', {
  code: REDIRECTION_AUTH_CODE
})
.then(function (response) {
  console.log(response);
})
.catch(function (error) {
  console.error(error);
});
© www.soinside.com 2019 - 2024. All rights reserved.