使用Google登录Javascript库强制用户批准提示
我的应用程序遵循中所述的服务器端流程,该流程为客户端提供一个一次性代码,该代码被转发到服务器,服务器在该服务器中获得使用Google登录Javascript库强制用户批准提示,javascript,google-signin,Javascript,Google Signin,我的应用程序遵循中所述的服务器端流程,该流程为客户端提供一个一次性代码,该代码被转发到服务器,服务器在该服务器中获得访问\u令牌和刷新\u令牌 在以前版本的Google Sign-In for Javascript库中,开发人员可以在设置库时传递属性'approval\u prompt':'force',以在用户单击Google Sign-In按钮时强制显示approval prompt。如果您需要再次获取刷新\u令牌,这可能非常有用,因为只有在向用户显示批准提示时,一次性代码才会授予刷新\u令
访问\u令牌
和刷新\u令牌
在以前版本的Google Sign-In for Javascript库中,开发人员可以在设置库时传递属性'approval\u prompt':'force'
,以在用户单击Google Sign-In按钮时强制显示approval prompt。如果您需要再次获取刷新\u令牌
,这可能非常有用,因为只有在向用户显示批准提示时,一次性代码才会授予刷新\u令牌
在以前的API中,我可以在调用
gapi.signin.render()
时传递'approval\u prompt':'force'
,以强制提示。在较新版本的API中(使用gapi.auth2.init()
),它似乎不尊重的“批准提示”:“强制”
。是否仍然可以使用Javascript库中较新的API,使用'approval\u prompt':'force'
或以前版本中类似的机制强制批准提示?为了使用gapi的'approval\u prompt':'force'
,您可以使用grantofleaccess
,它将考虑参数,而gapi.sign.render()
则不考虑参数
下面是一个小的身份验证示例,用于获取具有指定范围的脱机访问,使用'approval\u prompt':“force”
始终请求权限(并最终每次都获取新的刷新令牌):
谷歌认证
#客户电话{
显示:内联块;
背景:白色;
颜色:#444;
宽度:190px;
边界半径:5px;
边框:薄实线#888;
盒影:1px 1px 1px灰色;
空白:nowrap;
}
#自定义BTN:悬停{
光标:指针;
}
span.label{
字体系列:衬线;
字体大小:正常;
}
span.icon{
背景:url('https://developers.google.com/identity/sign-in/g-normal.png’)透明5px 50%无重复;
显示:内联块;
垂直对齐:中间对齐;
宽度:42px;
高度:42px;
}
span.buttonText{
显示:内联块;
垂直对齐:中间对齐;
左侧填充:42px;
右边填充:42px;
字体大小:14px;
字体大小:粗体;
/*使用中加载的Roboto字体*/
字体系列:“Roboto”,无衬线;
}
谷歌认证
使用以下方式登录:
谷歌
var-auth2;
函数start(){
加载('auth2',函数(){
auth2=gapi.auth2.init({
客户id:“您的客户id”,
范围:“配置文件”
});
});
$('#customBtn')。单击(函数(){
auth2.grantoflineaccess({'redirect_uri':'postmessage','approval_prompt':'force'});
});
}
函数signInCallback(authResult){
log(authResult['code']);
}
我不太清楚您在问什么-您是在问以前版本的API是否仍然有效?你有理由认为不会吗?你自己试过了吗?我更新了我的问题,希望能澄清我的问题
<!-- The top of file index.html -->
<html itemscope itemtype="http://schema.org/Article">
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="https://apis.google.com/js/platform.js?onload=start" parsetags="explicit" async defer></script>
<title>Google authentication</title>
<style type="text/css">
#customBtn {
display: inline-block;
background: white;
color: #444;
width: 190px;
border-radius: 5px;
border: thin solid #888;
box-shadow: 1px 1px 1px grey;
white-space: nowrap;
}
#customBtn:hover {
cursor: pointer;
}
span.label {
font-family: serif;
font-weight: normal;
}
span.icon {
background: url('https://developers.google.com/identity/sign-in/g-normal.png') transparent 5px 50% no-repeat;
display: inline-block;
vertical-align: middle;
width: 42px;
height: 42px;
}
span.buttonText {
display: inline-block;
vertical-align: middle;
padding-left: 42px;
padding-right: 42px;
font-size: 14px;
font-weight: bold;
/* Use the Roboto font that is loaded in the <head> */
font-family: 'Roboto', sans-serif;
}
</style>
</head>
<body>
<h1>Google Authentication</h1>
<hr>
<div id="gSignInWrapper">
<span class="label">Sign in with:</span>
<div id="customBtn" class="customGPlusSignIn">
<span class="icon"></span>
<span class="buttonText">Google</span>
</div>
</div>
<div id="log"></div>
<script>
var auth2;
function start() {
gapi.load('auth2', function () {
auth2 = gapi.auth2.init({
client_id: "YOUR_CLIENT_ID",
scope: 'profile'
});
});
$('#customBtn').click(function () {
auth2.grantOfflineAccess({'redirect_uri' : 'postmessage', 'approval_prompt' : 'force'}).then(signInCallback);
});
}
function signInCallback(authResult) {
console.log(authResult['code']);
}
</script>
</body>
</html>