【问题标题】:Passport js Google Ouath2 scopesPassport js Google Oauth2 范围
【发布时间】:2022-04-13 23:56:11
【问题描述】:

我有一个项目,这个项目有一个登录页面,我使用护照 js google-ouath20。

我需要性别、生日信息,这就是我在 google 上添加新范围的原因

router.get(
  "/login/google",
  passport.authenticate("google", { scope: [  "https://www.googleapis.com/auth/user.gender.read", "https://www.googleapis.com/auth/user.birthday.read", "https://www.googleapis.com/auth/user.addresses.read", "profile", "email" ] })
);

但毕竟,当我尝试获取信息时,我只能访问个人资料和电子邮件信息。我错了其中的哪一部分?

passport.use(
  new GoogleStrategy(
    {
      clientID: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
      callbackURL: GOOGLE_CALLBACK_URL,
      passReqToCallback: true,
    },

    async (req, accessToken, refreshToken, profile, cb) => {
      console.log("profile: ",profile) ...

【问题讨论】:

标签: node.js passport.js passport-google-oauth2


【解决方案1】:

您可以通过使用人作为API的文档中描述的得到这样的数据link: P>

  1. 启动在谷歌API控制台@ 987654322人民API @。

从人API获取数据,需要有效的API密钥 2.要创建API密钥:

  1. 导航到API和在云端控制台@ 987654323服务→凭证面板@ P>

  2. 选择创建凭证,从下拉菜单中然后选择API密钥。 P>

  3. 在API密钥创建对话框显示新创建的键。 P>

  4. 然后,执行一个简单的取 P>

https://people.googleapis.com/v1/people/(USER_ID)?personFields =(要字段)&密钥=(有效的API密钥)=的access_token(的accessToken在鉴别的结果credetials)

  • 这里是你如何能得到用户的数据:
    1. 添加对应数据范围阵列 LI>

    的列表范围link 2.添加名称字段这里?personFields=(fields you want) 3.终端用户必须检查框,如果他们希望他们的数据提供给您的应用程序

代码我用来获取用户的生日 P>

router.get(
        "/login/google",
        passport.authenticate("google")
    );

    // strategy code 

    const getbirthDay = async (id, accessToken) => {

       const response = await fetch(`https://people.googleapis.com/v1/people/${id}?personFields=birthdays&key=${process.env.GOOGLE_PEOPLE_API_KEY}&access_token=${accessToken}`);

        data.birthdays.map(entry => {
            const { year, month, day } = entry.date
             console.log(JSON.stringify(entry.date, undefined, 4));
        });

        // const { year, month, day } = data.birthdays[0].date
    }

    passport.use(
        new GoogleStrategy(
            {
                clientID: GOOGLE_CLIENT_ID,
                clientSecret: GOOGLE_CLIENT_SECRET,
                callbackURL: GOOGLE_CALLBACK_URL,
                passReqToCallback: true,
                scope: ['profile', 'email', 'https://www.googleapis.com/auth/user.birthday.read'],
                state: true
            },

            async (req, accessToken, refreshToken, profile, cb) => {
                const { id } = profile._json
                await getbirthDay(id, accessToken)

            }
        ))

【讨论】:

    猜你喜欢
    • 2021-12-11
    • 2015-05-13
    • 1970-01-01
    • 2017-06-17
    • 2018-11-29
    • 2013-08-18
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多