Facebook API - How do I get a Facebook user's profile image through the Facebook API (without requiring the user to Allow the application)
Simply fetch the data through this URL:
http://graph.facebook.com/userid_here/picture
Replace userid_here
with id of the user you want to get the photo of. You can also use HTTPS as well.
You can use the PHP's file_get_contents
function to read that URL and process the retrieved data.
Resource:
http://developers.facebook.com/docs/api
Note: In php.ini
, you need to make sure that the OpenSSL extension is enabled to use thefile_get_contents
function of PHP to read that URL.
Get user profile picture by Id
http://graph.facebook.com/" + facebookId + "/picture?type=square
For instance:
http://graph.facebook.com/67563683055/picture?type=square
There are also more sizes besides "square". See the docs.
Update September 2020
As Facebook have updated their docs this method is not working anymore without a token. You need to append some kind of access_token
. You can find further information and how to do it correctly in the fb docs to the graph api of user picture
Requirements Change
This endpoint supports App-Scoped User IDs (ASID), User IDs (UID), and Page-Scoped User IDs (PSID). Currently you can query ASIDs and UIDs with no requirements. However, beginning October 24, 2020, an access token will be required for all UID-based queries. If you query a UID and thus must include a token:use a User access token for Facebook Login authenticated requests
use a Page access token for page-scoped requests
use an App access token for server-side requests
use a Client access token for mobile or web client-side requests
Quote of fb docs
Getting profile picture with swift through Facebook Graph API return unsupported URL
Able to get it using this code:
// Get user profile pic
var fbSession = PFFacebookUtils.session()
var accessToken = fbSession.accessTokenData.accessToken
let url = NSURL(string: "https://graph.facebook.com/me/picture?type=large&return_ssl_resources=1&access_token="+accessToken)
let urlRequest = NSURLRequest(URL: url!)
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue()) { (response:NSURLResponse!, data:NSData!, error:NSError!) -> Void in
// Display the image
let image = UIImage(data: data)
self.imgProfile.image = image
}
Can't get facebook user's profile picture without access token
Depending on your platform, you can get the temporary access token
Android
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
accessToken = AccessToken.getCurrentAccessToken();
}
iOS
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *accessToken = [FBSDKAccessToken currentAccessToken];
}
Javascript
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
var accessToken = response.authResponse.accessToken;
}
} );
A much more detailed code snippet and explanation can be found here https://developers.facebook.com/docs/facebook-login/access-tokens
After you get the accessToken, You can then get the user's Facebook profile picture from the Graph API using https://graph.facebook.com/USER_ID/picture?access_token=ACCESS_TOKEN
, just as you said.
Related Topics
Setting the PHP Default Encoding to Utf-8
How to Check If an Array Contains a Specific Value in PHP
Converting Words to Numbers in PHP
PHP Fatal Error: Call to Undefined Function Mssql_Connect()
Is MySQL_Real_Escape_String() Broken
Accessing Dates in PHP Beyond 2038
Dompdf and Set Different Font-family
Why Is Using a MySQL Prepared Statement More Secure Than Using the Common Escape Functions
Difference in Accessing Arrays in PHP 5.3 and 5.4 or Some Configuration Mismatch
When Should I Use Memcache Instead of Memcached
What Is Autoloading; How to Use Spl_Autoload, _Autoload and Spl_Autoload_Register
Format Bytes to Kilobytes, Megabytes, Gigabytes
In PHP (≫= 5.0), Is Passing by Reference Faster
PHP: Recreate and Display an Image from Binary Data
Converting a Unix Timestamp to Formatted Date String
Best Way to Defend Against MySQL Injection and Cross Site Scripting