0

So habe ich Google Oauth Flow-Setup mit googleapis Lib, und ich scheinen nicht zu tog et eine refresh_token, nachdem ich oauthClient.getToken(code, ..) verwenden. Es gibt nur die access_token und die id_token zurück.Erhalte Refresh_Token mit googleapis Lib

Hier ist mein Ergebnis aus dem Code:

{ 
    access_token: "...", 
    expiry_date: 1475080667529, 
    id_token: "...", 
    token_type: "Bearer" 
} 

Hier ist mein Code:

function getOauth() { 
    var oauthClient = new google.auth.OAuth2(
    config.googleOauth.id, 
    config.googleOauth.secret, 
    `${config.https ? 'https' : 'http'}://${config.baseUrl}/google` 
); 
    return Bluebird.promisifyAll(oauthClient); 
} 

/** 
* Initialize session based of the auth code, which 
* gives us the accessToken and the payload. 
* 
* Returns a session with the user tokenized and 
* the accessToken for use to restore on page refresh. 
*/ 
router.get('/initial', function * (req, res) { 
    try { 
    let oauth = getOauth(); 
    let code = req.query.authorizationCode; 
    let results = yield oauth.getTokenAsync(code); 
    let tokens = results[0]; 
    let result = yield oauth.verifyIdTokenAsync(tokens.id_token, config.googleOauth.id); 
    let payload = result.getPayload(); 
    let user = yield User.findOneByEmail(payload.email, req.communityConfig); 
    let data = { user }; 

    if (user.role) { 
     let role = yield Role.findOne(user.role, req.communityConfig); 
     data.roles = [role]; 
    } 

    let token = auth.createToken(data); 

    res.json(extend({}, req.query, { token, accessToken: tokens.id_token })); 
    } catch(error) { 
    console.log(error.message); 
    throw new StatusError(401, error); 
    } 
}); 

/** 
* Use the google provider's fetch method to restore the session 
* data using the accessToken. 
* 
* Returns a token created from the user and role, along with 
* all of the query props passed in. 
*/ 
router.get('/restore', function * (req, res) { 
    try { 
    let oauth = getOauth(); 

    oauth.setCredentials({ 
     access_token: req.query.accessToken 
    }); 

    let tokens = yield oauth.getAccessTokenAsync(); 
    let result = yield oauth.verifyIdTokenAsync(tokens[0], config.googleOauth.id); 
    let payload = result.getPayload(); 
    let user = yield User.findOneByEmail(payload.email, req.communityConfig); 
    let data = { user }; 

    if (user.role) { 
     let role = yield Role.findOne(user.role, req.communityConfig); 
     data.roles = [role]; 
    } 

    let token = auth.createToken(data); 

    res.json(extend({}, req.query, { token })); 
    } catch(error) { 
    console.log(error.message); 
    throw new StatusError(401, error); 
    } 
}); 

So alles funktioniert, beim ersten und beim Aktualisieren, aber nach dem access_token abgelaufen ist, es nicht mehr authentisiert.

Token verwendet zu spät, 1475070618,739> 1475005777

ich gehört, dass ich access_type-'offline' angeben müssen, aber ich weiß nicht einmal, wo das in meinem Fall zu setzen, und ich dachte, das war für Dinge wie Hintergrundsynchronisation. Unter anderem ist es schwer zu testen, da es etwa einen Tag dauert, bis es abläuft.

Antwort

0

Wow, eine weitere Frage hier auf SO half mir mein Problem zu lösen.

Die Option access_type ist eine FRONTEND Option. Also in meinem Fall war es hier: https://github.com/Vestorly/torii/blob/master/addon/providers/google-oauth2.js und ich setze mir auf 'offline'.

Nach dem Zurückziehen des Zugriffs des Benutzers (hier https://security.google.com/settings/security/permissions) und der erneuten Zustimmung wurde refresh_token zurückgegeben.

Dieser Kommentar Not receiving Google OAuth refresh token von Zack-Morris hat wirklich geholfen.

Verwandte Themen