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

查看:17
本文介绍了如何通过 Meteor 的账户-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、姓名和性别.这似乎是我想要的,也是推荐的解决方案.唯一的问题是没有返回关于用户图片的数据.

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 的账户-facebook 查询 Facebook 用户图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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