无法在 google 操作中使用隐式/授权流程授权用户

Unable to Authorize users using Implicit / Authorization flow in google actions

我正在尝试 link 到帐户:

这是我的 google 云函数

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; 

在 google 操作控制台中 :

我已经创建了隐式流并给予了我的授权url如下:

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

错误:

这是浏览器Url

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

显示以下错误

The parameter "state" must be set in the query string.

更新 1

在开始此实施之前,我已按照 解决方案创建身份验证。

此方法中的问题:

1.As 在文档中声明它没有重定向到 google.com 并且我无法使用 [=67= 中的 APIAI SDK 访问 token ].但我仍然可以在模拟器中看到 Access token。为了更好地理解添加图像

这是我的模拟器O/P

{

  "response": {

  "debug": {
    "agentToAssistantDebug": {

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

更新 2:

所以我开始使用隐式流程进行创作,这是我的完整作品 repo

我认为问题在于这一行的 URL 没有将参数作为 query 参数发送,他们将它们作为 [=] 的一部分发送=16=]主播:

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')

在与它搏斗之后我已经实现了它,因为没有关于创建自己的 Oauth 服务器实现 Google 操作的适当文章,这可能对未来的用户有帮助。

授权端点

  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)
      })
  });

创建此端点后发布到 Google 云功能。我使用 MYSQL 作为使用 SEQUELIZEOauth-Server 的数据库,如果有人需要这些模型,将通过 repo 共享它。

有了这个,您可以 link 使用您自己的服务器来实现帐户 授权令牌和访问令牌