google api javascript get logged in user's email

拟墨画扇 提交于 2019-12-23 10:57:16

问题


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.

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.

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

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

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.

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

Thanks

来源:https://stackoverflow.com/questions/40185765/google-api-javascript-get-logged-in-users-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!