Paul Sham
Paul Sham

Reputation: 3205

Get Facebook user's profile picture prior to authenticating app

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!

Upvotes: 7

Views: 10959

Answers (4)

Hüseyin Cevizci
Hüseyin Cevizci

Reputation: 919

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.

Upvotes: 7

Joy Biswas
Joy Biswas

Reputation: 6527

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

Upvotes: 0

panzerdriver
panzerdriver

Reputation: 9

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.

Upvotes: 0

Naseer Panhwer
Naseer Panhwer

Reputation: 169

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

Upvotes: 2

Related Questions