2017-09-25 156 views
1

我想鏈接到的帳戶:無法對用戶進行授權使用谷歌的行動隱/授權流

這裏是我的谷歌的雲功能

var AuthHandler = function() { 
    this.googleSignIn = googleSignIn; 
    this.googleSignInCallback = googleSignInCallback; 

} 

function googleSignIn(req, res, next) { 
    passport = req._passport.instance; 

    passport.authenticate('google',{scope: 'https://www.googleapis.com/auth/userinfo.email', 
    state:"google",response_type:"token"}, 

    function(err, user, info) { 
console.log(user); 
    })(req,res,next); 

}; 

function googleSignInCallback(req, res, next) { 
    passport = req._passport.instance; 
    passport.authenticate('google',function(err, user, info) { 
     if(err) { 
      return next(err); 
     } 
     if(!user) { 
      return res.redirect('http://localhost:8000'); 
     } 
     console.log(user._json.token); 
     // /res.redirect('/'); 
     res.redirect('https://oauth-redirect.googleusercontent.com/r/xxxxxx#access_token=' + user._json.token + '&token_type=bearer&state=google') 


    })(req,res,next); 
}; 

module.exports = AuthHandler; 

在谷歌操作控制檯

我創建了隱式流程並授予了我的授權url,如下所示:

https://[region]-[projectid].cloudfunctions.net/[functionname]/auth/google 

錯誤:

這是瀏覽器的URL

https://assistant.google.com/services/auth/handoffs/auth/complete?state=xxxx&code=xxxxxx 

在其上顯示

以下錯誤參數 「狀態」 必須在查詢字符串設置。

更新1

開始執行此之前,我按照this解決方案創建的認證。

問題這種方法:

1.As在它不重定向到google.com的文檔說明,我無法使用JavaScript中的APIAI SDK訪問token。但我仍然可以在模擬器中看到Access token。爲了更好的理解添加圖像

Unable to redirect to the Google

這裏是我的模擬器O/P

{ 

    "response": { 

    "debug": { 
    "agentToAssistantDebug": { 

    "assistantToAgentDebug": { 
     "assistantToAgentJson": "{"accessToken\":\"xxxxxx\"" 
    } 
    }, 
    "errors": [] 
} 

更新2:

所以我已經開始與隱流動形成,這裏是我的完整repo

+0

我不清楚這些功能是什麼,或者是要求他們鏈接的帳戶。他們應該是OAuth2服務器嗎?他們應該被稱爲動作webhook嗎? – Prisoner

+0

@Prisoner,是的,我試圖創建Oauth服務器,我跟着你以前的答案[this](https://stackoverflow.com/questions/44288981/how-to-authenticate-user-with-just-谷歌帳戶上的行動),讓我更新這些細節有問題,以便您可以輕鬆地通過他們 – Webruster

回答

0

與之對抗後,我已經實現了它,因爲沒有關於創建實現Google Action的自己的Oauth服務器的適當文章,這對未來用戶可能會有所幫助。

授權端點

app.get('/authorise', function(req, res) { 
    req.headers.Authorization = 'Bearer xxxxxxxxxxx'; 
     // with your own mechanism after successful 
     //login you need to create a access token for the generation of 
    //authorization code and append it to this header; 

    var request = new Request(req); 
    var response = new Response(res); 

    oauth.authorize(request, response).then(function(success) {  
    // https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID? 
     //code=AUTHORIZATION_CODE&state=STATE_STRING 
     var toredirect = success.redirectUri +"?code="+success.code 
      +"&state="+request.query.state ; 
     return res.redirect(toredirect); 
    }).catch(function(err){ 
     res.status(err.code || 500).json(err) 
    }) }); 

令牌端點:

app.all('/oauth/token', function(req,res,next){ 
    var request = new Request(req); 
    var response = new Response(res); 

    oauth 
     .token(request,response) 
     .then(function(token) { 
     // Todo: remove unnecessary values in response 
     return res.json(token) 
     }).catch(function(err){ 
     return res.status(500).json(err) 
     }) 
    }); 

創建這個端點後發佈到谷歌雲功能。我使用MYSQL作爲數據庫使用SEQUELIZEOauth-Server,如果任何人需要這些模型,將通過回購分享。

有了這個,你可以使用自己的自己的服務器,它實現 驗證令牌和訪問令牌

+0

您提到了AUTHORIZATION_CODE - 但這是從哪裏來的? –

+1

@JasonSilver這是自動生成的資源服務 – Webruster

1

我認爲問題在於在這條線沒有發出參數查詢參數的URL,他們把他們作爲的一部分:?

res.redirect('https://oauth-redirect.googleusercontent.com/r/xxxxxx#access_token=' + user._json.token + '&token_type=bearer&state=google') 

您應該以替換#,如下圖所示:

res.redirect('https://oauth-redirect.googleusercontent.com/r/xxxxxx?access_token=' + user._json.token + '&token_type=bearer&state=google') 
+0

仍然是相同的錯誤。 – Webruster

+0

現在不同的錯誤消息「帳戶無法鏈接。請關閉瀏覽器並重試# – Webruster

+0

請更新原始問題以添加更改以及更改後的結果。您可能還想澄清一下,您正在做什麼來生成每個錯誤。 – Prisoner