无法使用Google行动中的隐式/授权流程对用户进行授权 [英] Unable to Authorize users using Implicit / Authorization flow in google actions
问题描述
我正在尝试链接到该帐户:
I am trying to link to the account :
这是我的 google cloud功能
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 Action Console中:
我创建了隐式流程,并给出了我的授权URL,如下所示:
I have created the implicit flow and gave my authorisation url as follows:
https://[region]-[projectid].cloudfunctions.net/[functionname]/auth/google
错误:
这是浏览器网址
https://assistant.google.com/services/auth/handoffs/auth/complete?state=xxxx&code=xxxxxx
显示以下错误
必须在查询字符串中设置参数状态".
The parameter "state" must be set in the query string.
更新1
Before starting this implementation , i have followed this Solution to create the Authentication.
此方法存在的问题:
1.如文档中所述,它不会重定向到google.com,并且我无法使用JavaScript中的APIAI
SDK访问token
.但是我仍然可以在模拟器中看到Access token
.以便更好地了解添加图片
1.As stated in the Documentation it is not redirecting to google.com and i'm unable to access the token
using the APIAI
SDK in javascript. but still i can see the Access token
in emulator . for better understanding adding images
这是我的模拟器O/P
Here is my simulator O/P
{
"response": {
"debug": {
"agentToAssistantDebug": {
"assistantToAgentDebug": {
"assistantToAgentJson": "{"accessToken\":\"xxxxxx\""
}
},
"errors": []
}
更新2:
所以我已经开始使用隐式流程进行创建,这是我完整的回购
So i have started creating with implicit flow and here is my complete repo
推荐答案
在与之抗争之后,我实现了它,因为没有关于创建自己的Oauth Server来实现Google Action的适当文章,这可能对将来的用户有所帮助
After battling with it i have achieved it , as there is no proper articles about creation of own Oauth Server that implements the Google Action , this might helpful for future users.
授权端点
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 Cloud函数.我已经使用MYSQL
作为使用SEQUELIZE
和Oauth-Server
的数据库,如果有人需要这些模型,将通过repo共享它.
After creation of this endpoints publish to the Google Cloud functions . I have used MYSQL
as the DB using SEQUELIZE
and Oauth-Server
, if anyone need those models , will share it through repo .
使用此功能,您可以使用自己的服务器链接帐户,该服务器实现了 身份验证令牌和访问令牌
With this you can able to link account using your own Server which implements Auth tokens and Access Tokens
这篇关于无法使用Google行动中的隐式/授权流程对用户进行授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!