JavaScript-Google身份验证令牌返回不包含refresh_token
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JavaScript-Google身份验证令牌返回不包含refresh_token,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3297字,纯文字阅读大概需要5分钟。
内容图文
![JavaScript-Google身份验证令牌返回不包含refresh_token](/upload/InfoBanner/zyjiaocheng/685/c77f3253fa8d483a83f65dff2e4db865.jpg)
我写了一个有关使用google api的示例. Google NodeJS Client library.我遵循指令集access_type:’offline’,但是对象返回不包含refresh_token.
我的代码:
var http = require('http');
var express = require('express');
var Session = require('express-session');
var google = require('googleapis');
var plus = google.plus('v1');
var OAuth2 = google.auth.OAuth2;
const ClientId = "251872680446-rvkcvm5mjn1ps32iabf4i2611hcg086e.apps.googleusercontent.com";
const ClientSecret = "F1qG9fFS-QwcrEfZbT8VmUnx";
const RedirectionUrl = "http://localhost:8081/oauthCallback";
var app = express();
app.use(Session({
secret: 'raysources-secret-19890913007',
resave: true,
saveUninitialized: true
}));
function getOAuthClient () {
return new OAuth2(ClientId , ClientSecret, RedirectionUrl);
}
function getAuthUrl () {
var oauth2Client = getOAuthClient();
// generate a url that asks permissions for Google+ and Google Calendar scopes
var scopes = [
'https://www.googleapis.com/auth/plus.me'
];
var url = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: scopes // If you only need one scope you can pass it as string
});
return url;
}
app.use("/oauthCallback", function (req, res) {
var oauth2Client = getOAuthClient();
var session = req.session;
var code = req.query.code;
oauth2Client.getToken(code, function(err, tokens) {
console.log("tokens : ", tokens);
// Now tokens contains an access_token and an optional refresh_token. Save them.
if(!err) {
oauth2Client.setCredentials(tokens);
session["tokens"]=tokens;
res.send(`
<html>
<body>
<h3>Login successful!!</h3>
<a href="/details">Go to details page</a>
<body>
<html>
`);
}
else{
res.send(`
<html>
<body>
<h3>Login failed!!</h3>
</body>
</html>
`);
}
});
});
app.use("/details", function (req, res) {
var oauth2Client = getOAuthClient();
oauth2Client.setCredentials(req.session["tokens"]);
var p = new Promise(function (resolve, reject) {
plus.people.get({ userId: 'me', auth: oauth2Client }, function(err, response) {
console.log("response : " , response);
resolve(response || err);
});
}).then(function (data) {
res.send(`<html><body>
<img src=${data.image.url} />
<h3>Hello ${data.displayName}</h3>
</body>
</html>
`);
})
});
app.use("/", function (req, res) {
var url = getAuthUrl();
res.send(`
<html>
<body>
<h1>Authentication using google oAuth</h1>
<a href=${url}>Login</a>
</body>
</html>
`)
});
var port = 8081;
var server = http.createServer(app);
server.listen(port);
server.on('listening', function () {
console.log(`listening to ${port}`);
});
解决方法:
在批准您指定的范围后,刷新令牌仅在用户首次登录到您的应用程序时发送一次.
编辑08/2018:使用rovaling_prompt:’强制’不再起作用,您需要使用提示:’同意'(检查@亚历山大的答案)
如果您想在每次用户登录时获取刷新令牌(即使用户之前已经登录并批准了范围),则必须在Oauth2Client配置中指定提示:“ consent”:
var url = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: scopes,
prompt : 'consent'
});
请注意,这将要求用户每次单击您的链接进行身份验证时都接受指定的范围:
您还可以在account permission settings中手动禁用该应用程序,这将撤消该应用程序,并且用户将不得不再次接受范围,这将触发在您下次进行身份验证时发送refresh_token:
仅供参考,如果您需要离线使用access_token,则必须存储refresh_token服务器端,并在收到Google API的状态401时使用存储的refresh_token刷新access_token.因此,如果按需存储refresh_token,则实际上无需使用提示:“同意”,并在用户每次连接到您的应用程序时强制用户批准范围.
内容总结
以上是互联网集市为您收集整理的JavaScript-Google身份验证令牌返回不包含refresh_token全部内容,希望文章能够帮你解决JavaScript-Google身份验证令牌返回不包含refresh_token所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。