meteor - accounts-github 包导致我的 meteor 用户有一个空的电子邮件

标签 meteor nullreferenceexception

我已经将 accounts-github 添加到我的 meteor 应用程序中,但是当我尝试访问 Meteor.user.services.github.email 时,我得到的都是空的。即使我知道电子邮件是在我的 github 帐户中设置的。我究竟做错了什么?该字段就在那里,好像 accounts-github 应该只是为我获取电子邮件...

最佳答案

来自 github api 文档:

Note: The returned email is the user’s publicly visible email address (or null if the user has not specified a public email address in their profile).

要获取私有(private)电子邮件地址,您需要将 user:email 范围添加到您的应用中。

如果您使用的是 accounts-ui,它只是

客户端

Accounts.ui.config({
    requestPermissions: {
        github: ['user:email']
    }
});

更新

我试过上面的代码,它给出了几个问题。看起来 github 不再将电子邮件数据与其他 OAuth 数据一起发送。添加这个 以及上面的(权限) 修复它:

它所做的是在对 github 的请求中单独获取电子邮件数据,并在用户登录时将其添加到您的用户。

添加github api包

meteor add mrt:github-api

服务器端代码

Accounts.onLogin(function(info) {
    var user = info.user;
    if(user) {

    var github = new GitHub({
          version: "3.0.0", // required
          timeout: 5000     // optional
      });

      github.authenticate({
        type: "oauth",
        token: user.services.github.accessToken
      });

      try {
        var result = github.user.getEmails({user: user.services.github.username});

        var email = _(result).findWhere({primary: true});

        Meteor.users.update({
          _id: user._id
        },
        {
          $set: {
            'profile.email': email.email,
            'services.github.email': email.email
          }
        })
      }
      catch(e) {
        console.log(e.message);
      }
    }
  });

然后您可以在 {{currentUser.profile.email}} (html), Meteor.user().profile.email 中正常访问电子邮件地址,以及 services.github 对象中。

这样做也有一个好处,如果他们在 github 上更改并重新登录,电子邮件字段将保持最新状态。

关于meteor - accounts-github 包导致我的 meteor 用户有一个空的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24689889/

相关文章:

c# - 什么是NullReferenceException,如何解决?

javascript - 我应该如何将 "es6 import"与来自客户端服务器或两者的高级方法 Meteor 包一起使用?

Meteor Iron 路由器无法从路由获取当前路径

meteor - 如何检测 Meteor 中 react 性元素何时更新

c# - 这怎么会抛出 NullReferenceException?

c# - 未从 UI 调用函数时用户为空

c# - 尝试在 formview 中绑定(bind)下拉列表时出现空引用异常

c# - 调用链内的空合并

meteor - 铁路由器和 meteor : Template rendering on different url

reactjs - React findOne 在客户端返回未定义