Feathers js - 如何通过谷歌登录

时间:2021-04-16 06:19:01

标签: node.js google-cloud-platform oauth feathersjs

我正在按照 doc 创建谷歌策略。

当我通过浏览器访问 http://localhost:3030/oauth/google 时,出现以下错误:

Error Code 400: redirect_uri_mismatch
The redirect URI in the request, https://localhost/oauth/google/callback, 
does not match the ones authorized for the OAuth client. 
To update the authorized redirect URIs, visit: https://console.developers.google.com/apis/credentials/oauthclient/12345678-xxxx.apps.googleusercontent.com?project=xxxxyyyy

enter image description here

authentication.js

const { AuthenticationService, AuthenticationBaseStrategy, JWTStrategy } = require('@feathersjs/authentication');
const { LocalStrategy } = require('@feathersjs/authentication-local');
const { expressOauth } = require('@feathersjs/authentication-oauth');

const axios = require('axios');
const { OAuthStrategy } = require('@feathersjs/authentication-oauth');

class GoogleStrategy extends OAuthStrategy {
  async getEntityData(profile) {
    const baseData = await super.getEntityData(profile);
    // this will grab the picture and email address of the Google profile
    return {
      ...baseData,
      email: profile.email
    };
  }
}

module.exports = app => {
  const authentication = new AuthenticationService(app);

  authentication.register('jwt', new JWTStrategy());
  authentication.register('local', new LocalStrategy());
  authentication.register('google', new GoogleStrategy());
 
  app.use('/authentication', authentication);
  app.configure(expressOauth());
};

config/local.json

{
    "authentication": {
      "entity": "user",
      "service": "users",
      "secret": "SA3c59SscyH6TscABCdeFG=",
      "authStrategies": [
        "jwt",
        "local",
        "google"
      ],
      "jwtOptions": {
        "header": {
          "typ": "access"
        },
        "audience": "https://yourdomain.com",
        "issuer": "feathers",
        "algorithm": "HS256",
        "expiresIn": "1d"
      },
      "local": {
        "usernameField": "email",
        "passwordField": "password"
      },
      "oauth": {
        "google": {
          "key": "xxx.apps.googleusercontent.com",
          "secret": "ASDFGgh"
        }
      }
    },
}

更新 1
通过在 https://localhost/oauth/google/callback 中添加 Authorized redirect URIs 解决了问题。

现在网站重定向到 Select Account 页面。

在我点击我的帐户后,网站被重定向到 https://localhost/oauth/google/callback?code=4/abcd-xxx-xxxxx-xxx&scope=email%20profile%20https://www.googleapis.com/auth/userinfo.email%20https://www.googleapis.com/auth/userinfo.profile%20openid&authuser=0&hd=myDomain.com&prompt=consent

enter image description here

更新 2

更新 local.json 并添加 redirect_uri 字段

      "oauth": {
        "redirect": "/",
        "google": {
          "redirect_uri": "http://localhost:3030/auth/google/callback", // add here
          "key": "abcd.googleusercontent.com",
          "secret": "xxxx",
          "scope": [
            "email",
            "profile",
            "openid"
          ],
          "nonce": true
        }
      }

现在可以重定向到 http://localhost:3030/auth/google/callback?code=xxx&scope=email+profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+openid&authuser=0&hd=myDomain.com&prompt=consent#

它显示 404 Page Not Find 而不是重定向到 / enter image description here

更新 3
现在网站重定向到 https://localhost/oauth/google/authenticate#,我知道它应该是 https://localhost:3030/oauth/google/authenticate,但我不知道 Feather/Google Cloud Platform 如何以及在哪里设置它

更新 4
终于成功重定向到www.google.com#error=Field%20password%20does%20not%20exist.%20(required), 这是一种错误吗?

    "oauth": {
      "redirect": "www.google.com",
      "google": {
        "key": "<Google OAuth key>",
        "secret": "<Google OAuth secret>",
        "scope": [
          "email",
          "profile",
          "openid"
        ],
        "nonce": true,
        "redirect_uri": "http://localhost:3030/oauth/google/callback",
        "callback": "/oauth/google/authenticate"
      }
    }

1 个答案:

答案 0 :(得分:2)

问题第一重定向uri

重定向 URI 必须与您发送的位置完全匹配

您的应用正在发送

https://localhost/oauth/google/callback

您仅将以下内容添加为有效的重定向 uri

http://localhost:3030/auth/google/callback

解决方案是获取 https://localhost/oauth/google/callback 并将其添加为 Google Developer Console 中的有效重定向 uri。

问题二。

无法访问网站。

您的应用程序已告诉谷歌您已准备好响应来自以下端点的授权服务器的授权代码

https://localhost/oauth/google/callback?code=4/abcd-xxx-xxxxx-xxx&scope=email%20profile%20https://www.googleapis.com/auth/userinfo.email%20https://www.googleapis.com/auth/userinfo.profile%20openid&authuser=0&hd=myDomain.com&prompt=consent

您的网站似乎无法处理该响应。我会检查回调文件是否存在。