簡體   English   中英

Feathers js - 如何通過谷歌登錄

[英]Feathers js - how to login by google

我正在按照文檔創建谷歌策略。

當我通過瀏覽器訪問 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

在此處輸入圖像描述

身份驗證.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());
};

配置/本地.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
通過在Authorized redirect URIs中添加https://localhost/oauth/google/callback解決了問題。

現在網站重定向到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

在此處輸入圖像描述

更新 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而不是重定向到/ 在此處輸入圖像描述

更新 3
現在網站重定向到https://localhost/oauth/google/authenticate# ,我知道它應該是https://localhost:3030/oauth/google/authenticate ,但我不知道如何以及在哪里 Feather/Google Cloud平台可以設置

更新 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"
      }
    }

問題一重定向 uri

重定向 URI 必須與您發送的位置完全匹配

您的應用正在發送

https://localhost/oauth/google/callback

您僅將以下內容添加為有效的重定向 uri

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

解決方案是采用 https://localhost/oauth/google/callback 並將其添加為 Google Developer 控制台中的有效重定向 uri。

問題二。

無法訪問站點。

您的應用程序已告訴 google,您已准備好響應來自以下端點的授權服務器的授權代碼

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

您的網站似乎無法處理該響應。 我會檢查回調文件是否存在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM