google api javascript登录用户的电子邮件

时间:2016-10-21 21:35:43

标签: javascript google-api

有许多资源和堆栈溢出问题与我要问的相似但不完全相同。我将在这里重新讨论一些解决方案并解释它们。

我有一位已登录Google的用户。登录后我的意思是手动登录并且存在cookie。我的应用程序未登录。

我只需要获取电子邮件地址。

有三种方法可以做到这一点我见过但不适用于我。

#p>方式#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);
  });
});
#p>方式#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堆栈溢出说你必须使用令牌而不是api密钥。但是用户已经登录,我没有,也无法获得令牌。

如何获取ALREADY登录用户的EMAIL?

由于

1 个答案:

答案 0 :(得分:2)

尽管有一个老问题..这可能会有所帮助..以防万一..

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()启动实例。基于实例化的内容,您可以使用其中任何一个来获取配置文件详细信息。

相关问题