簡體   English   中英

如何使用FB.api('/ me / picture')獲取個人資料圖片

[英]How to use FB.api('/me/picture') to get profile image

這是一個非常愚蠢的問題。 我是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')來顯示圖像。

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

如果您想要檢索URL並自己使用它,您需要通過以下方式專門請求它作為字段:

 /{user id}?fields=picture

要么

 /me/?fields=picture

你也可以包括其他字段,但我假設你現在只想要照片。

您還可以獲得個人資料照片的特定尺寸:

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

        console.log(response.data.url);

});  

請參閱Facebook文檔,了解您可以獲得的不同圖片尺寸

以及登錄的完整演示: 使用Javascript SDK獲取Facebook個人資料圖片

這肯定是拍攝的,證明可以使用facebook圖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>

FB.api('/me/picture')將返回重定向到圖片網址,因此對您沒用:
替代文字
您使用的方式有效,如文檔 (閱讀部分)中所述。 也許是相關的,但我不知道它是否完全有效。

我得到了Facebook個人資料圖片所有尺寸

個人資料圖片大小

值= 24 32 40 50 60 74 80 86 100 111 120 130 148 160 200 240 320 480 720 960 1440

卷曲

https://graph.facebook.com/v3.1/me?fields=picture.width(value).height(2000)&access_token={}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM