有许多资源和堆栈溢出问题与我将要问的问题相似但不完全相同。我将在这里重新讨论一些解决方案并进行解释。
我有一个已经登录 Google 的用户。登录是指手动登录并且存在 cookie。我的应用程序未登录。
我只需要获取电子邮件地址。
我见过 3 种方法可以做到这一点,但都不适合我。
方式#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());
}
方式#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);
});
});
方式#3:
gapi.client.load('oauth2', 'v2', function () {
gapi.client.oauth2.userinfo.get().execute(function (resp) {
// Shows user email
console.log(resp.email);
})
});
对于 Way #2 和 Way #3 堆栈溢出表示您必须使用 token 而不是 api key 。但是用户已经登录,我没有也无法获得 token 。
如何获取已经登录用户的 EMAIL?
谢谢
最佳答案
虽然是一个老问题..这可能会有所帮助..只是以防万一..
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() 启动。根据用于实例化的内容,您可以使用其中任何一个来获取配置文件详细信息。
关于javascript - google api javascript 登录用户的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40185765/