2015-05-09 41 views
16

Trong khi phát triển ví dụ cuối cùng của giới thiệu book ( ứng dụng sử dụng chiến lược xác thực của Google OpenID), sau thay thế gói passport-google(đã lỗi thời vào ngày 20 tháng 4 năm 2015) với gói passport-google-oauth2 (chiến lược xác thực của Google OAuth 2.0) và đã làm theo các chỉ dẫn tại its documentation's pageexample here; Tôi gặp lỗi bên dưới sau khi chọn tài khoản Google+ của mình, được ném bởi mô-đun oath2.js, cụ thể gọi this._oauth2.get("https://www.googleapis.com/plus/v1/people/me",...) trong phương thức userProfile(accessToken, done). Mã nguồn có liên quan và phụ thuộc mô-đun bên dưới.Node.js "hộ chiếu-google-oauth2" cung cấp lỗi "không tìm nạp hồ sơ người dùng" trong ứng dụng Express

Điều gì có thể là gốc của sự cố?

Các lỗi cụ thể là:

InternalOAuthError: failed to fetch user profile 
    at <...>\web-app\b4\node_modules\passport-google-oauth2\lib\oauth2.js:92:28 
    at passBackControl (<...>\web-app\b4\node_modules\passport-google-oauth2\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:124:9) 
    at IncomingMessage.<anonymous> (<...>\web-app\b4\node_modules\passport-google-oauth2\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:143:7) 
    at IncomingMessage.emit (events.js:129:20) 
    at _stream_readable.js:908:16 
    at process._tickCallback (node.js:355:11) 

mã ứng dụng có liên quan là:

passport = require('passport'), 
    //... 
    GoogleStrategy = require('passport-google-oauth2').Strategy; // #passport-google-oauth2 
    //... 
    /***** #passport-google-oauth2 vv *****/ 
    passport.use(new GoogleStrategy({ 
    clientID: "a_specific_value", 
    clientSecret: "another_specific_value", 
    callbackURL: "http://127.0.0.1:3000/auth/google/callback", 
    passReqToCallback:true 
    }, 
    function(request, accessToken, refreshToken, profile, done) { 
     profile.identifier=profile.id; 
     return done(null, profile); 
    } 
)); 
    /***** #passport-google-oauth2 ^^ *****/ 
    //... 
    /***** #passport-google-oauth2 vv *****/ 
    app.get('/auth/google', 
    passport.authenticate('google', { successRedirect: '/',scope: 
    [ 'https://www.googleapis.com/auth/userinfo.email']}) 
); 
    app.get('/auth/google/callback', 
    passport.authenticate('google', { 
     successRedirect: '/', 
     failureRedirect: '/' 
    })); 
    /***** #passport-google-oauth2 ^^ *****/  

Ứng dụng này có sự phụ thuộc như sau:

 
[email protected] 
├─┬ [email protected] 
│ ├─┬ [email protected] 
│ │ └── [email protected] 
│ └── [email protected] 
├─┬ [email protected] 
│ ├── [email protected] 
│ └── [email protected] 
├─┬ [email protected] 
│ ├── [email protected] 
│ ├─┬ [email protected] 
│ │ └── [email protected] 
│ ├─┬ [email protected] 
│ │ ├── [email protected] 
│ │ ├── [email protected] 
│ │ ├── [email protected] 
│ │ ├── [email protected] 
│ │ └── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├─┬ [email protected] 
│ │ └── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ └─┬ [email protected] 
│ └── [email protected] 
├─┬ [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├─┬ [email protected] 
│ │ └── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├─┬ [email protected] 
│ │ ├── [email protected] 
│ │ └── [email protected] 
│ └── [email protected] 
├─┬ [email protected] 
│ ├── [email protected] 
│ ├─┬ [email protected] 
│ │ └── [email protected] 
│ ├── [email protected] 
│ └─┬ [email protected] 
│ └── [email protected] 
├─┬ [email protected] 
│ └── [email protected] 
├─┬ [email protected] 
│ ├── [email protected] 
│ └── [email protected] 
├─┬ [email protected] 
│ └─┬ [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ └── [email protected] 
├── [email protected] 
├── [email protected] 
└─┬ [email protected] 
    ├── [email protected] 
    ├── [email protected] 
    ├── [email protected] 
    ├─┬ [email protected] 
    │ ├── [email protected] 
    │ └─┬ [email protected] 
    │ └── [email protected] 
    ├─┬ [email protected] 
    │ ├── [email protected] 
    │ ├── [email protected] 
    │ ├── [email protected] 
    │ └── [email protected] 
    ├─┬ [email protected] 
    │ ├── [email protected] 
    │ ├── [email protected] 
    │ └── [email protected] 
    ├── [email protected] 
    ├── [email protected] 
    ├── [email protected] 
    ├── [email protected] 
    ├── [email protected] 
    └── [email protected] 

Trả lời

49

Tôi chỉ may mắn tìm thấy một vấn đề tương tự tại jaredhanson/passport-google-oauth, đã cho tôi ý tưởng để truy cập vào Google's project console và chỉ cần bật Google+ API, đã bị tắt "(oh me !!, nhà phát triển ngây thơ của ứng dụng đầu tiên của anh ấy dựa trên Google+). Đó là gốc rễ của vấn đề. Tôi đã thử lại và oauth2 bắt đầu nhận hồ sơ chính xác.

+1

Đó chính xác những gì nó đã cho tôi quá, cảm ơn! – Nocturno

+1

Tôi cũng đã thay đổi các phạm vi không được chấp nhận 'https: // www.googleapis.com/auth/userinfo.profile' và' https: // www.googleapis.com/auth/userinfo.email' thành 'hồ sơ' và' email 'tương ứng. – Samin

+0

wow cảm ơn đã dành rất nhiều thời gian cho việc này. họ thực sự nên trả lại một số loại lỗi! cho tôi gọi lại sẽ treo –

4

Các scope bạn đang sử dụng bị phản đối tại:

passport.authenticate('google', { successRedirect: '/',scope: 
    [ 'https://www.googleapis.com/auth/userinfo.email']}) 
); 

Thay vào đó, chúng ta phải sử dụng cái này:

passport.authenticate('google', { successRedirect: '/',scope: 
    ['email'] 
})); 

Bạn cũng có thể nhận được profile scope:

passport.authenticate('google', { successRedirect: '/',scope: 
    [ 'email', 'profile' ] 
})); 
+0

Tôi gặp vấn đề tương tự với Github oauth. Bất cứ ý tưởng gì còn thiếu? – Sandeep

Các vấn đề liên quan