在meteor oauth中访问github用户数据

时间:2015-02-27 16:30:53

标签: javascript oauth meteor

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

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

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

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访问用户?

1 个答案:

答案 0 :(得分:1)

请看一下这个示例代码,你是否在onCreateUser上捕获了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