如何使用FB.api('/ me / picture')获取个人资料图片

时间:2011-01-12 22:53:13

标签: facebook facebook-graph-api

这是一个非常愚蠢的问题。我是facebook Javascript SDK的初学者。 所以我试图让用户的个人资料图片显示出来 我用过这段代码

FB.api('/me', function(response) {      
    document.getElementById('login').style.display = "block";
    document.getElementById('login').innerHTML = '<img src="http://graph.facebook.com/' + response.id + '/picture" />';
});

工作正常,但我正在尝试了解如何使用FB.api('/me/picture')来显示图片。

5 个答案:

答案 0 :(得分:13)

/me/picture(或/{user id}/picture)会将HTTP 301重定向返回到图片位置,以便您可以将其直接嵌入<img src...

如果您想要检索URL并自己使用它,您需要通过以下方式专门请求它:

 /{user id}?fields=picture

 /me/?fields=picture

您也可以包含其他字段,但我假设您现在只想要照片。

答案 1 :(得分:9)

您也可以获得个人资料照片的特定尺寸:

FB.api("/me/picture?width=180&height=180",  function(response) {

        console.log(response.data.url);

});  

请参阅Facebook documentation to see what different picture sizes you can get

一个完整的演示,登录时为:Get Facebook Profile Picture with Javascript SDK

答案 2 :(得分:1)

这是肯定的镜头,证明可以使用facebook graph api 2.5。 这是示例HTML请参阅我在FB.api()函数中所做的更改。

 <!DOCTYPE html>
    <html>
    <head>
    <title>Facebook Login JavaScript Example</title>
    <meta charset="UTF-8">
    </head>
    <body>

    <!--
      Below we include the Login Button social plugin. This button uses
      the JavaScript SDK to present a graphical Login button that triggers
      the FB.login() function when clicked.
    -->
    <img src="" id="profileImage"/>

    <div id="status">
    </div>

    </body>
    <script>

        // This is called with the results from from FB.getLoginStatus().
        function statusChangeCallback(response) {
            console.log('statusChangeCallback');
            console.log(response);
            // The response object is returned with a status field that lets the
            // app know the current login status of the person.
            // Full docs on the response object can be found in the documentation
            // for FB.getLoginStatus().
            if (response.status === 'connected') {
                // Logged into your app and Facebook.
                testAPI();
            } else if (response.status === 'not_authorized') {
                // The person is logged into Facebook, but not your app.
                document.getElementById('status').innerHTML = 'Please log ' +
                        'into this app.';
            } else {
                // The person is not logged into Facebook, so we're not sure if
                // they are logged into this app or not.
                document.getElementById('status').innerHTML = 'Please log ' +
                        'into Facebook.';
            }
        }

        // This function is called when someone finishes with the Login
        // Button.  See the onlogin handler attached to it in the sample
        // code below.
        function checkLoginState() {
            FB.getLoginStatus(function(response) {
                statusChangeCallback(response);
            });
        }

        window.fbAsyncInit = function() {
            FB.init({
                appId      : 'XXXXXXXXXXXX',
                cookie     : true,  // enable cookies to allow the server to access
                                    // the session
                xfbml      : true,  // parse social plugins on this page
                version    : 'v2.5' // use graph api version 2.5
            });

            // Now that we've initialized the JavaScript SDK, we call
            //FB.getLoginStatus().  This function gets the state of the
            // person visiting this page and can return one of three states to
            // the callback you provide.  They can be:
            //
            // 1. Logged into your app ('connected')
            // 2. Logged into Facebook, but not your app ('not_authorized')
            // 3. Not logged into Facebook and can't tell if they are logged into
            //    your app or not.
            //
            // These three cases are handled in the callback function.

            FB.getLoginStatus(function(response) {
                statusChangeCallback(response);
            });

        };

        // Load the SDK asynchronously
        (function(d, s, id) {
            var js, fjs = d.getElementsByTagName(s)[0];
            if (d.getElementById(id)) return;
            js = d.createElement(s); js.id = id;
            js.src = "//connect.facebook.net/en_US/sdk.js";
            fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));

        // Here we run a very simple test of the Graph API after login is
        // successful.  See statusChangeCallback() for when this call is made.
        function testAPI() {
            console.log('Welcome!  Fetching your information.... ');
            FB.api('/me', function(response) {
                console.log('Successful login for: ' + response.name);
                console.log('Successful login for: ' + response.id);
                console.log('Successful login for: ' + response.email);
                var im = document.getElementById("profileImage").setAttribute("src", "http://graph.facebook.com/" + response.id + "/picture?type=normal");
                document.getElementById('username').innerHTML =response.name;
            });
        }

    </script>
    </html>

答案 3 :(得分:0)

FB.api('/me/picture')会将redirect返回到图片网址,因此对您没有用处:
alt text
您使用的方式有效,如documentation(阅读部分)中所述。也许this是相关的,但我不知道它是否完全有效。

答案 4 :(得分:0)

我得到了所有尺寸的Facebook个人资料图片

个人资料图片大小

值= curl -i https://api.sejda.com/v1/tasks\ --fail --silent --show-error \ --header "Content-Type: application/json" \ --data '{"htmlCode": "<strong>HTML<\/strong> code here", "type": "htmlToPdf" }' > converted.pdf 24 32 40 50 60 74 80 86 100 111 120 130 148 160 200 240 320 480 720 960

CURL

1440