2016-02-27 94 views
1

我想在我的應用中使用simple-oauth2在node.js中授權gmail登錄,並試圖獲取用戶配置文件,最初我正在進行身份驗證,代碼是使用OAuth2獲取node.js中的刷新令牌和gmail用戶配置文件

var oauth2 = require('simple-oauth2')({ 
    clientID: configAuth.googleAuth.clientID, 
    clientSecret: configAuth.googleAuth.clientSecret, 
    site: 'https://accounts.google.com/o/', 
    tokenPath: 'https://accounts.google.com/o/oauth2/token', 
    authorizationPath: 'oauth2/auth' 
}); 

//授權URI定義

var authorization_uri = oauth2.authCode.authorizeURL({ 
    redirect_uri: 'http://localhost:8080/auth/google/callback', 
    scope: 'https://www.googleapis.com/auth/userinfo.profile', 
    scope: 'https://www.googleapis.com/auth/analytics.readonly', 
    scope: 'https://www.googleapis.com/auth/userinfo.email', 
    state: '382332$' 
}); 
app.get('/auth/google', function (req, res) { 
    res.redirect(authorization_uri); 
}); 

回調是,

app.get('/auth/google/callback', function (req, res) { 
    var code = req.query.code; 
    oauth2.authCode.getToken({ 
     code: code, 
     redirect_uri: 'http://localhost:8080/auth/google/callback' 
    }, saveToken); 

    function saveToken(error, result, profile1) { 
     if (error) { 
      console.log('Access Token Error', error); 
     } 
     token = oauth2.accessToken.create(result); 
     console.log('token',token) 
    } 

令牌的結果是這樣的:

{ access_token: 'ya29.lQKN8a2ECM0h5ECWL4NcThS924m_eMUWg9I3BVfRvkTiK-lt-  8_1bCLjBmtmi68Kg8k', 
    token_type: 'Bearer', 
    expires_in: 3599, 
    id_token: 'somelongstring', 
    expires_at: Sat Feb 27 2016 16:38:05 GMT+0530 (India Standard Time) 
    } 

在令牌響應我無法得到刷新令牌。有沒有其他方法來獲取刷新令牌?

還有如何獲得登錄用戶的個人資料的詳細信息,一旦登錄成功。請提出一些解決方案..! 在此先感謝!

回答

0

我添加了這些

approval_prompt: 'force', 
access_type: 'offline', 
在authorization_uri像

var authorization_uri = oauth2.authCode.authorizeURL({ 
    redirect_uri: 'http://localhost:8080/auth/google/callback', 
    approval_prompt: 'force', 
    access_type: 'offline', 
    scope: 'https://www.googleapis.com/auth/userinfo.profile  
    state: '3832$' 
}); 

這爲我工作!

相關問題