有许多资源和堆栈溢出问题与我将要问的类似但不完全相同。我将在这里重新讨论一些解决方案并进行解释。
我有一个已经登录 Google 的用户。我所说的登录是指手动登录并且存在 cookie。我的应用程序未登录。
我只需要获取电子邮件地址。
我见过 3 种方法可以做到这一点,但都不适合我。
Way #1:
auth2 = gapi.auth2.getAuthInstance();
if (auth2.isSignedIn.get()) {
var profile = auth2.currentUser.get().getBasicProfile();
console.log('ID: ' + profile.getId());
console.log('Full Name: ' + profile.getName());
console.log('Given Name: ' + profile.getGivenName());
console.log('Family Name: ' + profile.getFamilyName());
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail());
}
Way #2:
gapi.client.setApiKey(API_KEY);
gapi.client.load("plus", "v1", function() {
gapi.client.plus.people.get({ userId: "me" }).execute(function(resp) {
// Shows profile information
console.log(resp);
});
});
Way #3:
gapi.client.load('oauth2', 'v2', function () {
gapi.client.oauth2.userinfo.get().execute(function (resp) {
// Shows user email
console.log(resp.email);
})
});
对于方式 #2 和方式 #3 堆栈溢出表示您必须使用令牌而不是 api 密钥。但用户已经登录,我没有也无法获取令牌。
如何获取已登录用户的电子邮件?
Thanks
虽然是一个老问题..这可能会有所帮助..以防万一..
var auth2 = gapi.auth2.getAuthInstance();
var profile = auth2.currentUser.get().getBasicProfile();
console.log(profile.getName());
console.log(profile.getEmail());
该实例可以通过gapi.auth2.getAuthInstance() 或gapi.auth2.init() 启动。根据用于实例化的内容,您可以使用其中之一来获取配置文件详细信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)