0

認證代碼中發展的問題,直到今年年初完美的工作,然後看起來打破了我的一點點改變,我試圖倒退到以前的版本,並找到沒有運氣的原因。所以我正在尋求幫助修復今天存在的代碼。invalid_request丟失:使用谷歌Passportjs谷歌Oauth2作用域

我使用nodejs v0.10.25 Passportjs通過Google和Facebook提供身份驗證。我的包:

"config-multipaas": "^0.1.0", 
"restify": "^2.8.3", 
"googleapis": "~2.1.5", 
"mocha": "~2.3.3", 
"restify-redirect": "~1.0.0", 
"sequelize": "~3.12.2", 
"mysql": "~2.9.0", 
"passport": "~0.3.2", 
"passport-facebook": "~2.0.0", 
"passport-local": "~1.0.0", 
"passport-google-oauth": "~0.2.0", 
"sendgrid-webhook": "0.0.4", 
"sendgrid": "~2.0.0", 
"restify-cookies": "~0.2.0" 

兩個星期前,在應用程序的另一部分工作時,我注意到,用戶註冊功能不再工作了兩種服務。具體來說,Google Oauth2代碼在初始許可頁面後返回以下錯誤。

  1. That’s an error.

Error: invalid_request

Missing required parameter: scope

這裏是我的策略定義的相關部分:

passport.use(new FacebookStrategy({ 
    clientID:  siteConfigs.facebook.clientId, 
     clientSecret: siteConfigs.facebook.clientSecret, 
     callbackURL: authRoot + 'facebook/callback', 
    profileFields: ['id','name','emails', 'picture', 'location', 'birthday', 'gender'] 
    }, 
    function(accessToken, refreshToken, profile, done){...} 

passport.use(new GooglePlusStrategy({ 
    clientID:  siteConfigs.google.clientId, 
     clientSecret: siteConfigs.google.clientSecret, 
     callbackURL: authRoot + 'google/callback', 
    profileFields: ['id','name','emails', 'gender', 'placesLived'] 
    }, 
    function(accessToken, refreshToken, profile, done){...} 

的令牌回調函數的機制是不相關的進程從來沒有得到那麼遠(由節點調試器和控制檯日誌報表驗證)。

這裏是我的休息端點:

app.get('/auth/facebook', passport.authenticate('facebook', { session: false, scope: ['email']})); 

app.get('/auth/google', passport.authenticate('google', 
     { 
     scope: ['profile', 'email'], 
     accessType: 'online', 
     session: false 
     }) 
); 

app.get('/auth/:service/callback', function(req, res, next){ 
    console.log('Request:', req.path(), req.getQuery()); 

    passport.authenticate(req.params.service, 
    function(err, user, info){ 
     var returnPath = '/html/authcallback.html'; 

     if(err) 
      return res.redirect({ 
       pathname: returnPath, 
       query: { 
        error: err 
       } 
      }, next); 
     else 
      return res.redirect({ 
       pathname: returnPath, 
       query: { 
        id: user.id, 
        created: info 
       } 
      }, next); 
    })(req, res, next); 
}); 

一些初步的故障排除之後,我花了使用谷歌的OAuth 2.0遊樂場定製的身份驗證和令牌端點(在我的passportjs版本所使用的那些)的方法和我的擁有自己的客戶端ID和密碼,以便將每個交互與我的代碼進行比較。最初的同意提示請求正常工作,並且與Playground匹配,並且回調將返回適當的代碼,例如code=4/EUnaIvWpLIiHnKUR9WctOJKQ-_iWZ3_H9YeUYx7bJSo。但交換令牌代碼的請求失敗。使用節點調試器,評估重定向請求護照谷歌-的OAuth發回的令牌,它建立的網址是:

https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=http://localhost:8080/auth/google/callback&client_id=<ID>

與此相比,從谷歌的Oauth 2.0遊樂場類似的請求,這將看起來像:

http://www.googleapis.com?code=4/9uaUYjeExmPftgRLsRZ8MCc2e_7YGHow7kvew6Fkypo&redirect_uri=https://developers.google.com/oauthplayground&client_id=<ID>&client_secret=<SECRET>&scope=&grant_type=authorization_code

有幾個參數,從我的查詢字符串缺少,兩個最重要的是我的代碼和祕密。我原以爲Google的服務會爲這些人返回一個錯誤。相反,它會爲流程中的這一步實際不需要的缺失範圍參數返回一個錯誤(我不知道操場應用程序爲什麼包含它)。

最後,我相當肯定它在Facebook戰略開始失敗的時候也在我的身邊。經過同意後,它回到我的回調多個代碼的奇怪,然後最終返回一個TOO_MANY_REDIRECTS錯誤。

那麼,任何人有任何想法?再一次,這個相同的代碼一直工作到年初,然後在一段時間後開始失敗。

+0

這已經上漲了一個半星期了。只是想知道有沒有人有任何想法。 –

+0

我自己解決了這個問題。社區絕對沒有答案。當我試圖與passportjs開發人員/維護人員分享問題鏈接時,他的迴應是嘗試銷售合同服務來解決問題。 考慮關閉該問題。 –

+0

感謝分享(不)。 –

回答

0

幾周前我問了this same question,偶然發現了一個答案。我很高興分享它:

Seems I was missing the following code after initializing the server:

api.use(restify.plugins.queryParser({ mapParams: false })); 

Once I added that line, I no longer saw the Google error, and the process passed back to my code.