我想自定义范围以仅允许“电子邮件”和“个人资料”,没有“openid”
因为我想让它只要求访问电子邮件和基本个人资料信息。
我尝试使用元来做到这一点:
<meta name="google-signin-scope" content="email profile">
或js:
gapi.auth2.init({
client_id: 'xxxxxxxxx.apps.googleusercontent.com',
scope: 'email profile'
});
但它不起作用:在生成的 URL 中始终存在“openid”范围...
我如何“重置”范围,并只允许我想要的内容?
BasicProfile 需要“个人资料电子邮件 openid”范围。
您可以禁用基本个人资料并仅需要“个人资料电子邮件”范围:
<meta name="google-signin-fetch_basic_profile" content="false">
<meta name="google-signin-scope" content="profile email">
请注意,在这种情况下,GoogleUser.getBasicProfile() 将返回 null。您必须手动获取“个人资料”和“电子邮件”数据。
完全有效的示例(您必须将 YOUR_CLIENT_ID 替换为您的实际 client_id):
<html>
<head>
<meta name="google-signin-client_id" content="YOUR_CLIENT_ID">
<meta name="google-signin-fetch_basic_profile" content="false">
<meta name="google-signin-scope" content="profile email">
</head>
<body>
<script>
function requestEmailData() {
gapi.client.load('oauth2', 'v2', function() {
gapi.client.oauth2.userinfo.get().execute(function(resp) {
// Shows user email
console.log(resp.email);
})
});
}
function requestProfileData() {
gapi.client.load('plus', 'v1', function() {
gapi.client.plus.people.get( {'userId' : 'me'} ).execute(function(resp) {
// Shows profile information
console.log(resp);
})
});
}
function onSuccess() {
gapi.load('client', function() {
// based on http://stackoverflow.com/a/15384981
requestEmailData();
requestProfileData();
});
}
</script>
<div class="g-signin2" data-onsuccess="onSuccess"></div>
<script src="https://apis.google.com/js/platform.js?onload=onLoad" async defer></script>
</body>
</html>
请注意,您必须在开发者控制台中启用 Google+ API 才能请求“个人资料”数据。要做到这一点
- go to https://console.developers.google.com/ https://console.developers.google.com/
- 选择项目
- 转到 API 和身份验证 > API
- 找到并启用 Google+ API。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)