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

标签 javascript google-api

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

我有一个已经登录 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/

相关文章:

javascript - 在 Javascript 中使用 Google Calendar API 插入方法创建新日历返回错误 400 "Missing title"

javascript - 语法错误 - 当前未启用对实验性语法 'decorators-legacy' 的支持

jquery - 如何消除 JQuery 库中不必要的重排?

iphone - 我们可以将 Google API 和 YouTube API 用于我们的商业视频流产品吗?

javascript - 如何比较两个对象 typescript angular6

oauth-2.0 - POSTMAN、OAuth2 和 Google Directory API

google-api - 提高了 Google Drive API 每用户限制,但仍然收到 userRateLimitExceeded 错误

javascript - 如何使用 react-grid-layout 创建动态拖放布局

javascript - 如何在点击另一个页面时执行一个页面的javascript

javascript - 使用 ES2016 在 React 中的箭头函数中调用 setState 时未安装组件