7

Summary:

I am working on a website that connects to Facebook through the Graph API. I was asked whether I can get a user's profile picture if they are already logged into Facebook, but prior to the user authenticating the app. Is this possible?

EDIT: I know you can get people's profile picture by going to http://graph.facebook.com/USERNAME_OR_USERID/picture?type=large, but would I be able to get a user's username or ID if they are logged into Facebook, but have not authorized my app?


Details:

According to the Facebook API User documentation, you can get their Facebook ID without an access_token, and I know if you go directly to a user's /me URL you can see their basic information. With the Facebook ID, I can grab their profile pic.

However, when I try to call it from the website using the following code, I get an OAuthException error.

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

I'm guessing that they have to authenticate the app, but I'm looking to see if I may have missed a way of getting their profile pic.

Thanks for your help!

1

4 Answers 4

7

You can get everyone's Facebook profile picture without authentication.

You just call http://graph.facebook.com/USERID/picture?type=large. This URL redirects you to image URL.

Edit: Querying by username is not allowed anymore, so I updated USERNAME_OR_USERID as USERID.

3
  • Thanks, cebrici. I know about this URL, but how would I get their username or ID without them authenticating?
    – Paul Sham
    Dec 16, 2011 at 14:29
  • 2
    I figured out that you can't get their username or ID without authenticating. Your answer was better written so I'll give you the answer.
    – Paul Sham
    Dec 16, 2011 at 15:06
  • I am using this is url, but its redirecting to a page with a blank image with a 'question mark'. Can you help me ? May 4, 2016 at 6:00
2

yes you can get..

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
1
  • 1
    Thanks, Naseer. I know about this URL, but how would I get their username or ID without them authenticating?
    – Paul Sham
    Dec 16, 2011 at 14:29
0

You can generate an access token using an application id and secret as follows:

https://graph.facebook.com/oauth/access_token?client_id={appID}&client_secret={appSecret}&grant_type=client_credentials

NOTE: Don't do this on the client side as you risk having your application getting hijacked.

The resulting token can be user to query users with the graph API without a login prompt.

0

How to get FB images w/o oauth or access tokens:

I was trying to get only images of few members but I was stuck then wrote this snippet and worked for me,

The HTML tag

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

The PHP code

<?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;
    }
?>

I only had to use a 30x30 image and I got what I wanted. Check out it also provides you a large image.

Cheers! Joy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.