【问题标题】:How to use Google OAuth to authenticate on the backend?如何使用 Google OAuth 在后端进行身份验证?
【发布时间】:2017-10-26 11:25:18
【问题描述】:

我有一个使用 OAuth 2 登录 Google 的 React。我还有一个创建用户的 node/express REST api。如何使用登录 React 应用时收到的数据对用户进行身份验证?

我一直在尝试关注这个:https://developers.google.com/identity/sign-in/web/backend-auth 但是我在登录时从谷歌获得的令牌会在一小时后过期,所以在那之后我不能用它来验证后端的用户。那么,如何获取新的token或者不重新登录就不会过期的token呢?

我确信这是在互联网上一百万个不同的网站上完成的,但我找不到任何好的文档或指南。

【问题讨论】:

  • 嗨!我很久以前就实现了 OAuth 授权,我记得技巧是将范围参数设置为“离线”,这样您就可以从刷新令牌中生成新令牌,看看developers.google.com/identity/protocols/…
  • 我还是很困惑。刷新访问令牌需要刷新令牌。第 5 步只返回一个新的 id_token(不是它所说的 refresh_token)。即使这样做了,刷新访问令牌也不会说它提供了新的 id_token。所以我可以得到一个新的(从第 5 步开始)id_token,但只有一个,并且会在一小时内到期。所以我还是卡住了。

标签: node.js authentication google-oauth


【解决方案1】:

首先,使用 Passport 获取 accesstoken 和 refreshtoken。当应用程序收到错误访问令牌过期时,您应该使用刷新令牌获取新的访问令牌。

- 获取 google oauth accesstoken 和 refreshtoken 的护照。

var passport         = require( 'passport' )
var GoogleStrategy   = require( 'passport-google-oauth2' ).Strategy;
var GOOGLE_CLIENT_ID      =  "xxxx"
  , GOOGLE_CLIENT_SECRET  = "xxxx";
passport.use(new GoogleStrategy({
    clientID:     GOOGLE_CLIENT_ID,
    clientSecret: GOOGLE_CLIENT_SECRET,
    callbackURL: "/auth/google/callback",
    passReqToCallback   : true
  },
  function(request, accessToken, refreshToken, profile, done) {
    console.log('accessToken: ', accessToken);
    console.log('refreshToken: ', refreshToken);    
  }
));
app.get('/auth/google', passport.authenticate('google', { scope: [
       'https://www.googleapis.com/auth/plus.login'
      ],  accessType: 'offline', prompt: 'consent'
}));
app.get( '/auth/google/callback', 
        passport.authenticate( 'google', { 
            successRedirect: '/',
            failureRedirect: '/login'
}));

- 使用刷新令牌获取新的访问令牌

var requestREST = require('request');
requestREST.post('https://accounts.google.com/o/oauth2/token', {
      form: {
        grant_type:'refresh_token',
        refresh_token: refreshToken,
        client_id: GOOGLE_CLIENT_ID,
        client_secret:GOOGLE_CLIENT_SECRET
      }
    }, function (err, res, body) {
      console.log(body)
    });

享受吧!

【讨论】:

  • 我的后端也有一个类似的系统,也使用了 passport-google-oauth。我的问题是,如果我想使用基本的 http 请求手动验证客户端怎么办(例如,如果客户端不允许生成新的浏览器窗口或重定向)。有没有办法从客户端本身获取code 参数并将其传递给护照中的回调,而不是让护照将您重定向到谷歌的登录页面。
  • 如果您想访问开发者特定的资源,例如google cloud bucket,您可以使用REST api 获取accessToken,无需用户操作。仔细阅读官方文档,应该创建一个正确的 JWT 令牌。developers.google.com/identity/protocols/OAuth2ServiceAccount
  • 如果您想访问开发者特定的资源,例如google cloud bucket,您可以使用REST api 获取accessToken,无需用户操作。仔细阅读官方文档,应该创建一个正确的JWT令牌。developers.google.com/identity/protocols/OAuth2ServiceAccount在google官方文档中,它包含一些错误。主机名应该是accounts.google.com/o/oauth2/token 而不是googleapis.com/oauth2/v4/token,“aud”参数也应该是accounts.google.com/o/oauth2/token
  • 这里是卷曲。 curl -X POST \ accounts.google.com/o/oauth2/token \ -H 'content-type: application/x-www-form-urlencoded' \ -d 'grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9。 eyJpc3……'
猜你喜欢
  • 2011-10-20
  • 2012-11-27
  • 2021-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多