在验证应用之前获取Facebook用户的个人资料照片

时间:2011-12-15 23:33:28

标签: facebook facebook-graph-api facebook-javascript-sdk

要点:

我正在开发一个通过Graph API连接到Facebook的网站。有人问我是否可以获得用户的个人资料图片,如果他们已经登录到Facebook,但在用户验证应用程序之前。这可能吗?

修改 我知道你可以通过转到http://graph.facebook.com/USERNAME_OR_USERID/picture?type=large来获取人们的个人资料图片,但是如果他们登录到Facebook但未授权我的应用程序,我是否可以获得用户的用户名或ID?


详细信息:

根据Facebook API User documentation,您可以在没有access_token的情况下获取他们的Facebook ID,我知道如果您直接转到用户/我的URL,您可以看到他们的基本信息。通过Facebook ID,我可以获取他们的个人资料照片。

但是,当我尝试使用以下代码从网站上调用它时,我收到OAuthException错误。

FB.api('/me', function(response) {
    console.log(response);
});

我猜他们必须对应用进行身份验证,但我希望看看我是否错过了获取个人资料照片的方法。

感谢您的帮助!

4 个答案:

答案 0 :(得分:7)

您无需身份验证即可获取每个人的Facebook个人资料照片。

您只需致电http://graph.facebook.com/USERID/picture?type=large。 此网址会将您重定向到图片网址。

修改:不再允许按用户名进行查询,因此我将USERNAME_OR_USERID更新为USERID

答案 1 :(得分:2)

是的,你可以得到..

http://graph.facebook.com/naseer.panhwer/picture?type=normal
http://graph.facebook.com/naseer.panhwer/picture?type=small
http://graph.facebook.com/naseer.panhwer/picture?type=large

答案 2 :(得分:0)

您可以使用应用程序ID和密码生成访问令牌,如下所示:

https://graph.facebook.com/oauth/access_token?client_id= {APPID}&安培; client_secret = {appSecret}&安培; grant_type = client_credentials

注意:不要在客户端执行此操作,因为您可能会将应用程序劫持。

生成的令牌可以是用户使用图形API查询用户而无需登录提示。

答案 3 :(得分:0)

如何获取没有oauth或访问令牌的FB图像:

我试图只获得少数成员的图像,但我被困住然后写了这个片段并为我工作,

HTML标记

<img src="getFbImage.php?id=<fbid_of_the_person>&type=[small|large]" />

PHP代码

<?php

    $remoteImage = !isset($_GET['id']) ? "../images/default.png" : getImg($_GET['id']);
    $imginfo = getimagesize($remoteImage);
    header("Content-type: ".$imginfo['mime']);
    readfile($remoteImage);

    function getImg($id){
        $dom = new DOMDocument;
        $dom->loadHTML(getData($id));
        $tags = $dom->getElementsByTagName('img');
        $img = '';
        foreach ($tags as $tag) {
            $img =$tag->getAttribute('src');
            if($img=='')
                continue;
            else if(isset($_GET['type']) && $_GET['type']=='large')
                break;
        } 
        //This iteration has a lot of images try your luck.
        return $img;
    }

    function getData($id){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,'https://www.facebook.com/photo.php?fbid='.$id);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 Firefox/39.0');
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
?>

我只需使用30x30图像,我得到了我想要的东西。看看它也为你提供了一个大图像。

干杯! 喜悦

相关问题