javascript - 检索 Facebook 个人资料图片 URL

标签 javascript angularjs node.js facebook facebook-graph-api

我在 MEAN 应用程序中使用 satellizer 进行身份验证。身份验证完成后,我想获取用户的个人资料图片。以下是我的代码。

Angular Controller

(function(){
    'use strict';
    angular
        .module('nayakans07')
        .controller('RegisterController', registerController);

    function registerController ($scope, $auth) {

        $scope.authenticate = function (provider) {
            $auth.authenticate(provider)
                .then(function (res) {
                    console.log(res);
                }, authErrorHandler);
        };

        function authErrorHandler (err) {
            console.log(err);
        }
    }

})();

Node.js 路由处理程序

(function () {
    'use strict';

    var request = require('request');
    var qs = require('querystring');
    var tokenHelper = require('../helpers/tokenHelper.js');
    var config = require('../config/configuration.js');
    var Member = require('../models/memberModel.js');

    module.exports = function (req, res) {
        var accessTokenUrl = 'https://graph.facebook.com/oauth/access_token';
        var grapApiProfileUrl = 'https://graph.facebook.com/me';
        var graphApiProfileImageUrl = 'https://graph.facebook.com/me/picture?width=250&json=true';

        var params = {
            client_id: req.body.clientId,
            redirect_uri: req.body.redirectUri,
            code: req.body.code,
            client_secret: config.FACEBOOK_SECRET
        };

        request.get({
            url: accessTokenUrl,
            qs: params
        }, function (err, response, accessToken) {
            accessToken = qs.parse(accessToken);
            request.get({
                url: grapApiProfileUrl,
                qs: accessToken,
                json: true
            }, function (err, response, profile) {
                console.log(profile);
                Member.findOne({
                    facebookId: profile.id
                }, function (err, existingMember) {
                    if (existingMember) {
                        return res.status(200).send({
                            user: existingMember,
                            token: tokenHelper.createToken(existingMember, req, res)
                        });
                    } else {
                        request.get({
                            url: graphApiProfileImageUrl,
                            qs: accessToken,
                        }, function (err, response, profileImage) {
                            console.log(profileImage);
                            var fbData = {
                                facebookId: profile.id,
                                fullName: profile.name,
                                email: profile.email,
                                profileImage: profileImage
                            };
                            return res.status(200).send(fbData);
                        });
                    }
                });
            });
        });
    };


})();

从 Facebook 获取访问 token 后,我调用带有访问 token 的 https://graph.facebook.com/me/picture?width=250&json=true 端点来获取个人资料图片关联。在Facebook's API Explorer使用 token 调用相同的端点我得到配置文件图像 url 但是当我从 node.js 端点处理程序调用它时我得到一些二进制数据。部分回复在这里。

����JFIF���Photoshop 3.08BIM�gtc2XjTON-90Jqu9JFj83(bFBMD01000ac0030000b909000069110000fe120000dc14000069190000c02400002326000029280000872a0000fd3e0000��ICC_PROF

如何获取个人资料图片的 URL 而不是此响应。

最佳答案

不确定您正在使用的 API 版本是否仍然如此,但如果未传递正确的参数,它过去常常会导致 301 重定向到图像(尽管他们的 API Explorer 建议... ) 您可以检查浏览器的网络监控以验证这一点。

这有多种来源,包括官方 Facebook Graph-API 文档,其中指出:

By default this edge will return a 302 redirect to the picture image. To get access to the data about the picture, please include redirect=false in your query.

(来源:https://developers.facebook.com/docs/graph-api/reference/user/picture/#Reading)

连同一些(较旧但相似的)SO 答案:https://stackoverflow.com/a/7881978/624590

您有几个选择。一种是添加 redirect=false 查询参数(根据上面的 Facebook 文档)。

另一种选择是通过添加额外的查询参数&fields=url 来明确请求 URL(我上面链接的答案建议使用 &fields=picture,但到目前为止据我所知,这已经过时了)。如果您需要响应中的高度和宽度,只需将它们添加到查询中:&fields=url,height,width。根据我使用 Facebook API 的经验,明确询问字段往往是一种更可靠的方法。

关于javascript - 检索 Facebook 个人资料图片 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32036728/

相关文章:

javascript - Javascript 中带有全局变量的有趣作用域问题

node.js - 具有异步/等待的 Node - 如何获取发生错误的特定行?

javascript - 如何在html中按下按钮时显示图像?

javascript - Gooddata Javascript API - 不支持属性

javascript - Uncaught Error : Module 'myApp' is not available! (AngularJS)

angularjs - 如何设置 e2e Protractor 后端请求模拟/ stub

javascript - 填充动画延迟

javascript - If-Modified-Since 被 fetch API 忽略

javascript - 向输入字段添加自定义验证

node.js - 在 Node.js 中将 csv 文件读入多维数组的具体步骤是什么?