无法使用护照获取OAuth2Startegy的刷新令牌

时间:2020-01-27 12:33:26

标签: node.js oauth-2.0 passport.js

我正在尝试为Google Analytics(分析)报告API实施OAuthStrategy,为此我需要刷新令牌,但是我将刷新令牌设为未定义,如果有人知道这一点,请帮帮我。

const computeConnectedUser = strategy => (req, accessToken, refreshToken, profile, done) => {

    console.log('accessToken =>', accessToken);
    console.log('refreshToken =>', refreshToken);// undefined

    return done(false, { strategy, accessToken, refreshToken, ...profile });
}

const oAuth2Strategy = new OAuth2Strategy({
    authorizationURL: 'https://accounts.google.com/o/oauth2/v2/auth',
    tokenURL: 'https://www.googleapis.com/oauth2/v4/token',
    clientID: 'xxxxxx',
    clientSecret: 'xxxxxx',
    callbackURL: "http://localhost:3000/auth/google/callback",
    passReqToCallback: true
}, computeConnectedUser('oauth2'));

passport.use(oAuth2Strategy);

app.get('/auth/google', passport.authenticate('oauth2', {
    authType: 'rerequest',
    accessType: 'offline',
    prompt: 'consent',
    session: false,
    includeGrantedScopes: true,
    scope: ['https://www.googleapis.com/auth/analytics.readonly',
    'https://www.googleapis.com/auth/analytics'
    ]
}));

app.get('/auth/google/callback',
  passport.authenticate('oauth2', { session: false, failureRedirect: '/auth/google' }),
  function(req, res) {
    // Successful authentication, redirect home.
    console.log('res.body=>', res.accessToken);
    return res.status(200).json({ token: res.accessToken });
});

0 个答案:

没有答案