在具有现有访问令牌的 FeathersJs 中使用 Google Oauth

Using Google Oauth in FeathersJs with an existing access token

如何使用现有访问令牌在 featherjs 中使用 Google Oauth? docs do not give an example on this. The only example is through the browser as shown here.

通过浏览器时,http://localhost:3030/oauth/google 正常。我可以成功地将用户添加到我的数据库中。这是我的代码:

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

class GoogleStrategy extends OAuthStrategy {
  async getEntityData(profile) {
    const baseData = await super.getEntityData(profile);
    console.log({ profile });

    return {
      ...baseData,
      profilePicture: profile.picture,
      email: profile.email,
      password: uniqid.time(),
    };
  }
}

module.exports = (app) => {
const authentication = new AuthenticationService(app);        
authentication.register("local", new LocalStrategy());
authentication.register('jwt', new JWTStrategy());
authentication.register("google", new GoogleStrategy());

  app.use("/authentication", authentication);
  app.configure(expressOauth());
};

但是,如果我尝试使用访问令牌,就像这样

POST http://localhost:3030/authentication
data: {
    "strategy": "google",
    "accessToken": "ya29.A0ARrdaM_UJa6idfZr-4taqwkJ6qGBV1Dp9wbxF-wsult8dNPaVNCVg6Fndmrqv7BhRSwxa5gAKllPvbKtsjyxS39WdmWmqkmE42HOsVZaJWHVEttxbebel3zdpD5BSxWtRiG7NuZLNVedMUaK5AdgIRrJk1u"
}

我没有获得 google 配置文件,没有用户添加到我的数据库中,我收到此错误:

{
  "name": "GeneralError",
  "message": "401 Unauthorized",
  "code": 500,
  "className": "general-error",
  "data": {},
  "errors": {}
}

我已将 "google" 添加到 default.json

中的 authStrategies 列表中

所以我的问题是我需要做什么?

所以我找到了解决方案。以为我可以分享。将此添加到 authentication.js

 //add this method
 async getProfile(authResult) {
    const accessToken = authResult.accessToken;

    const { data } = await axios
      .get(
        `https://openidconnect.googleapis.com/v1/userinfo?access_token=${accessToken}`
      )
      .then((res) => {
        return res;
      })
      .catch((error) => console.log("autherr", error));

    return data;
}