2013-07-29 58 views
2

我目前正在尋求實現一個谷歌的API,使用客戶端的NodeJS谷歌PassportJS認證:使用與谷歌的NodeJS API庫

https://github.com/google/google-api-nodejs-client/

我試圖用護照,以便進行身份驗證,這似乎是工作的@

passport.use(new GoogleStrategy({ 
    clientID: GOOGLE_CLIENT_ID, 
    clientSecret: GOOGLE_CLIENT_SECRET, 
    callbackURL: "http://localhost:3000/auth/google/callback" 
}, 
function(accessToken, refreshToken, profile, done) { 
    process.nextTick(function() { 

     var user = { 
     id: profile.id, 
     email: profile.email, 
     firstName: profile.given_name, 
     lastName: profile.family_name, 
     accessToken: accessToken 
     }; 

     return done(null, user); 
    }); 
    } 
)); 

在我的谷歌身份驗證的回調:

app.get('/auth/google/callback', 
    passport.authenticate('google', { failureRedirect: '/login' }), 
    function(req, res) { 

    //do something here with the google api 

    }); 

我能得到的req.user舉行,這有accessToken

不過,對於谷歌API客戶端的NodeJS的文檔是不是在如何使用的accessToken清楚。 包含的示例顯示OAauth2Client檢索標記,但我猜這部分已經使用Passport進行了覆蓋?

+0

是的,一旦Passport調用成功回調,它成功並具有'profile',所以我認爲它已經完成了所有比較令牌或其他任務的工作。 – Plato

回答

4

我不熟悉google-api-nodejs-client,但是這是他們的文檔中:

oauth2Client.credentials = { 
    access_token: 'ACCESS TOKEN HERE', 
    refresh_token: 'REFRESH TOKEN HERE' 
}; 

client 
    .plus.people.get({ userId: 'me' }) 
    .withAuthClient(oauth2Client) 
    .execute(callback); 

我相信你可以設置憑據通過護照提供的,事情會正常工作。老實說,雖然我發現這些API包裝真的很人造,建議只使用request。這樣,您可以使用熟悉的模塊訪問任何 API服務任意提供程序。

+1

我確認,這項工作只是'從'googleapis'導入谷歌; const plus = google.plus('v1'); const oauth2Client = new google.auth.oauth2Client;' – standup75