如何通过Meteor的accounts-facebook查询Facebook用户的图片? [英] How to query a Facebook user picture via Meteor's accounts-facebook?

查看:96
本文介绍了如何通过Meteor的accounts-facebook查询Facebook用户的图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获得认证的Facebook用户的个人资料图片,以便在Meteor应用程序中使用。我尝试了以下

I'm trying to get the authenticated Facebook user's profile picture, to use within a Meteor application. I've tried the following

Meteor.publish("facebook_avatar_url", function() {
    return Meteor.users.find({_id: this.userId}, {fields: {
        'services.facebook.id': 1,
        'services.facebook.name': 1,
        'services.facebook.gender': 1,
        'services.facebook.picture': 1,
        'services.facebook.picture.data': 1,
        'services.facebook.picture.data.url': 1
    }});
});

,它只返回id,name和gender。这似乎是我想要的,和推荐的解决方案。只有问题是没有关于用户的图片返回的数据。

and it only returns the id, name, and gender. This seems to be what I want, and the recommended solution. Only problem is there's no data being returned regarding the user's picture.

我尝试将以下内容添加到server / server.js中,根据其他一些帖子的建议,但是a)似乎不是推荐的方法,而且b)似乎没有做任何事情。所以,似乎是一个死胡同,但有人在那里似乎认为它可以用来获得配置文件图片加载。

I tried adding the following to server/server.js, at the suggestion of some other posts, but it a) doesn't seem to be the recommended approach, and b) doesn't seem to be doing anything. So, it seems to be a dead end, but somebody out there seems to think that it can be used to get the profile picture loaded up.

var getFbPicture;

Accounts.loginServiceConfiguration.remove({
    service: "facebook"
});


Accounts.onCreateUser(function(options, user) {
    if (options.profile) {
        options.profile.picture = getFbPicture(user.services.facebook.accessToken);
        user.profile = options.profile;
    }
    return user;
});

getFbPicture = function(accessToken) {
    var result;
    result = Meteor.http.get("https://graph.facebook.com/me", {
        params: {
            access_token: accessToken,
            fields: 'picture'
        }
    });
    if (result.error) {
        throw result.error;
    }
    return result.data.picture.data.url;
};

所以,在这一点上我有点不确定要走哪个方向。这是需要在Facebook Graph API上设置权限的东西吗?还是在Facebook应用程序?发布功能中有错误的语法吗?我需要重新访问onCreateUser函数吗?

So, I'm kinda unsure which direction to go at this point. Is this something that needs permissions set on the Facebook Graph API? Or on the Facebook Application? Do I have the syntax wrong in the publication function? Do I need to revisit the onCreateUser function?

推荐答案

使用这个代替,您不需要访问令牌或任何特殊的方式来获取他们的个人资料图片,只是他们的Facebook用户ID。

Use this instead, you don't need an access token or anything special to get their profile picture, just their facebook user id.

Accounts.onCreateUser(function(options, user) {
    if (options.profile) {
        options.profile.picture = "http://graph.facebook.com/" + user.services.facebook.id + "/picture/?type=large";
        user.profile = options.profile;
    }
    return user;
});

这篇关于如何通过Meteor的accounts-facebook查询Facebook用户的图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆