帐户github包导致我的meteor用户的电子邮件为空
我已经在meteor应用程序中添加了帐户github,但当我尝试访问meteor.user.services.github.email时,我得到的所有帐户都是空的。即使我知道电子邮件是在我的github帐户中设置的。我做错了什么?该字段就在那里,似乎github帐户应该只为我获取电子邮件…来自github api文档: 注意:返回的电子邮件是用户的公开可见电子邮件地址(如果用户未在其配置文件中指定公开电子邮件地址,则为空) 要获取私人电子邮件地址,您需要将帐户github包导致我的meteor用户的电子邮件为空,meteor,nullreferenceexception,Meteor,Nullreferenceexception,我已经在meteor应用程序中添加了帐户github,但当我尝试访问meteor.user.services.github.email时,我得到的所有帐户都是空的。即使我知道电子邮件是在我的github帐户中设置的。我做错了什么?该字段就在那里,似乎github帐户应该只为我获取电子邮件…来自github api文档: 注意:返回的电子邮件是用户的公开可见电子邮件地址(如果用户未在其配置文件中指定公开电子邮件地址,则为空) 要获取私人电子邮件地址,您需要将user:email范围添加到应用程序中
user:email
范围添加到应用程序中
如果您使用的是accounts ui
,那么
客户端
Accounts.ui.config({
requestPermissions: {
github: ['user:email']
}
});
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);
}
}
});
更新
我试过上面的代码,它给出了几个问题。github似乎不再将电子邮件数据与其他OAuth数据一起发送。添加此项以及上述(权限)可修复此问题:
它所做的是在对github的请求中单独获取电子邮件数据,并在用户登录时将其添加到用户中
添加githubapi包
meteor add mrt:github-api
服务器端代码
Accounts.ui.config({
requestPermissions: {
github: ['user:email']
}
});
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上更改电子邮件字段并再次登录,电子邮件字段将保持最新状态。尝试了此操作,但电子邮件仍然为空。我做了流星重置从零开始,但仍然无法得到它。我看到登录时的github弹出窗口具有新的权限(我还添加了一些其他权限来测试它)。@Ben经过一点修补后,我让它工作了:)-更新的回答我稍后会尝试一下,谢谢你的帮助,但它不应该只是开箱即用吗?例如,谷歌软件包的账户确认了这一点。尽管如此,我还是希望我不必这么做。你应该在accounts github上做一个pull请求,这样做是开箱即用的…是的,这似乎可以通过accounts github包中的上游修复来帮助。让我们在Meteor问题跟踪器上打开一个支持请求,看看如何解决这个问题。