【问题标题】:accessing github user data in meteor oauth在流星 oauth 中访问 github 用户数据
【发布时间】:2015-04-30 10:39:22
【问题描述】:

我正在尝试使用 github 对用户进行身份验证,然后将他们的 avatar_url 传递给客户端。简化结构如下所示。

server/
  publications.js
client/
  users/
    login.js
  main.js

在我的client/users/login.js 文件中,我尝试获取包含头像 url 的用户对象的权限

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

然后在我的server/publications.js,我尝试发布the data related to the avatar url

Meteor.publish('userData', function() {
  if(this.userId) {
    return Meteor.users.find(
      { _id: this.userId }, {
      fields: {
        'services.github.id': 1,
        'services.github.user.avatar_url': 1
      }
    })
  } else {
    this.ready();
  }
});

但是,当我获得用户对象时,我从未获得与 github 用户相关的数据。如何使用 OAuth 访问用户?

【问题讨论】:

    标签: javascript oauth meteor


    【解决方案1】:

    请看一下这个示例代码,你是否在 CreateUser 上捕获了 Github 配置文件数据?

    编辑:这是服务器端代码,例如服务器/accounts.js

    Accounts.onCreateUser(function (options, user) {
        var accessToken = user.services.github.accessToken,
            result,
            profile;
        result = Meteor.http.get("https://api.github.com/user", {
            params: {
                access_token: accessToken
            }
        });
        if (result.error)
            throw result.error;
    
        profile = _.pick(result.data,
            "login",
            "name",
            "avatar_url",
            "url",
            "company",
            "blog",
            "location",
            "email",
            "bio",
            "html_url");
    
        user.profile = profile;
    
        return user;
    });
    

    Code found here

    【讨论】:

    • 这是服务器端的吗?
    • 非常有帮助,谢谢先生。如果我希望它在每次登录时都运行,我可以将它附加到不同的事件处理程序吗?
    • 如果用户选择不公开显示他们的电子邮件,上述返回的结果电子邮件字段将为空。如果您确实需要该电子邮件,则需要向 /user/emails 发送另一个 http 请求才能访问该电子邮件地址。
    猜你喜欢
    • 2016-01-29
    • 2016-07-17
    • 2018-02-08
    • 2015-05-06
    • 2016-04-27
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多