作者:mobiledu2502859097 | 来源:互联网 | 2023-06-08 12:55
在获取登录的客户端文件后,我是这样做的:// HTML 我将gapi.load()函数调用到 HTML 按钮中// load the startApp function after the page
在获取登录的客户端文件后,我是这样做的:
// HTML
我将gapi.load()
函数调用到 HTML 按钮中
// load the startApp function after the page loads
jQuery(function () {
$(window).load(function () {
startApp()
})
})
var startApp = function () {
gapi.load('auth2', function () {
// Retrieve the singleton for the GoogleAuth library and set up the client.
auth2 = gapi.auth2.init({
client_id: 'xxxxxxxx-xxxxxxxxxx.apps.googleusercontent.com',
COOKIEpolicy: 'single_host_origin',
ux_mode: 'redirect', // I don't want it to display a pop-up
scope: 'profile email' // I just need to get user's name, profile picture and email address
});
// attach this function into a button element with id = "customBtn"
attachSignin(document.getElementById('customBtn'));
});
};
function attachSignin(element) {
auth2.attachClickHandler(element, {},
function (googleUser) {
// it never calls this block of code.
// this never runs
console.log(googleUser.getBasicProfile().getName())
var gProfile = googleUser.getBasicProfile();
var name = gProfile.getName();
var email = gProfile.getEmail();
var imgUrl = gProfile.getImageUrl();
}, function (error) {
return alert("Google Sign in error!")
});
}
它将必要的功能加载到一个按钮中。如果用户点击该按钮,用户将被重定向到 Google 的登录页面。用户成功登录后,Google 会将 URL 重定向回我的网站。
它应该也发送用户的个人资料信息到我的attachClickHandler()
内部功能attachSignin()
。但它永远不会发生,因为它会在调用处理程序函数之前重新加载页面。仅当我将其更改ux_mode: 'redirect'
为默认值时才有效'popup
我现在能做的最好的事情就是从token_id
登录后 Google 在 URL 中提供的参数中获取电子邮件地址。id_token
URL 中的字段是一个可以解码的 jwt
http://localhost:3006/login#scope=email%20profile%20https://www.googleapis.com/auth/userinfo.email%20https://www.googleapis.com/auth/userinfo.profile%20openid&id_token=xxxxxxxxx&client_id=xxxxxxxxxxxx-xxxxxx.apps.googleusercontent.com
那么如何通过ux_mode
set to获取用户的个人资料信息redirect
呢?