How can I display the users profile pic using the facebook graph api?
Knowing the user id the URL for their profile picture is:-
http://graph.facebook.com/[UID]/picture
where in place of [UID] you place your $uid variable, and that URL can be passed to flash
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
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.
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.
how to set a facebook profile picture using the graph api
Upload the picture to an existing album (or create a new one) using the Graph API.
Will look something like this:
$args = array('message' => 'Caption');
$args['image'] = '@' . realpath("the_image.png");
try {
$data = $facebook->api('/'.$album_uid.'/photos', 'post', $args);
}
catch(Exception $e) {
print "<pre>";
print_r($e);
print "</pre>";
}
Then get the uploaded image via the Graph API and redirect to the image's link, add &makeprofile=1
to the querystring. The user will now be redirected to the profile image cropping page:
try {
$pictue = $facebook->api('/'.$data['id']);
header("Location: ".$pictue['link']."&makeprofile=1");
}
catch(Exception $e) {
print "<pre>";
print_r($e);
print "</pre>";
}
Get user image from Facebook Graph-API
ImageView user_picture;
userpicture=(ImageView)findViewById(R.id.userpicture);
URL img_value = null;
img_value = new URL("http://graph.facebook.com/"+id+"/picture?type=large");
Bitmap mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
userpicture.setImageBitmap(mIcon1);
Where ID is one your profile ID.
For Further details check this Reference for Graph API
............................
Related Topics
How to Generate a Custom Menu/Sub-Menu System Using Wp_Get_Nav_Menu_Items in Wordpress
Installing Pdo Driver on MySQL Linux Server
Remove/Replace the Username Field with Email Using Fosuserbundle in Symfony2/Symfony3
Laravel 5:Class 'Input' Not Found
Best Way to Initialize (Empty) Array in PHP
Guzzle 6: No More JSON() Method for Responses
JSON VS. Serialized Array in Database
Why Would I Use Dirname(_File_) in an Include or Include_Once Statement
How to Learn About PHP Internals
What Is Sapi and When Would You Use It
How to See the Extensions Loaded by PHP
Laravel - Seeding Relationships
What's the Best Way to Create a Single-File Upload Form Using PHP
PHP Function with Optional Parameters
How to Save Changed Simplexml Object Back to File