google api javascript获取用户的电子邮件登录 [英] google api javascript get logged in user's email

查看:104
本文介绍了google api javascript获取用户的电子邮件登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多资源和堆栈溢出问题,这些问题与我要问的相似但又不完全相同.我将在这里重新介绍一些解决方案并进行解释.

There are many resources and stack overflow questions that are similar but not exactly the same as what I will ask. I will rehash some of the solutions here and explain them.

我有一个已经登录Google的用户.登录是指手动登录并且存在cookie.没有通过我的应用程序登录.

I have a user that is already logged into Google. By logged in I mean manually logged in and the cookie is present. Not logged in by my application.

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

I just need to get the email address.

我已经知道有3种方法可以做到这一点,但是对我来说都不可行.

There are 3 ways to do this that I have seen but neither works for me.

方法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);
  })
});

对于方法2和方法3堆栈溢出,您必须使用令牌而不是api密钥.但是该用户已经登录,我没有登录,也无法获取令牌.

For Way #2 and Way #3 stack overflow says that you have to use a token and not an api key. But the user is already logged in and I don't have and cannot obtain the token.

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

How to get the EMAIL of an ALREADY logged in user?

谢谢

推荐答案

尽管有一个古老的问题..这可能会有所帮助..只是为了以防万一.

Though an old question.. this may help .. just incase..

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

The instance can be initiated by either gapi.auth2.getAuthInstance() or gapi.auth2.init(). Based on what is used to instantiate you can use either to get profile details.

这篇关于google api javascript获取用户的电子邮件登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆