Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set a facebook profile picture using the graph api

Is there any way to change the user's profile picture using the graph api?

I know you can't with the rest api (reference), but I could not find anything in the new graph api.

like image 930
Mauro Zadunaisky Avatar asked Nov 29 '10 18:11

Mauro Zadunaisky


People also ask

How do I create an app ID on Facebook Graph API?

Go to “My apps” drop down in the top right corner and select “add a new app”. Choose a display name and a category and then “Create App ID”. Again get back to the same link developers.facebook.com/tools/explorer. You will see “Graph API Explorer” below “My Apps” in the top right corner.

How to use Facebook Graph API to extract data?

From this documentation, choose any field you want from which you want to extract data such as “groups” or “pages” etc. Go to examples of codes after having selected these and then select “facebook graph api” and you will get hints on how to extract information.

What is the purpose of the profile object in the API?

The profile object is used within the Graph API to refer to the generic type that includes all of these other objects. The individual reference docs for each profile type should be used instead. When something has a profile, Graph API returns the fields for that object.

How do I get a Facebook API Token?

From “Graph API Explorer” drop down, select your app. Then, select “Get Token”. From this drop down, select “Get User Access Token”. Select permissions from the menu that appears and then select “Get Access Token.”. Go to link developers.facebook.com/tools/accesstoken. Select “Debug” corresponding to “User Token”. Go to “Extend Token Access”.


2 Answers

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>";
}
like image 89
fredrik Avatar answered Oct 18 '22 18:10

fredrik


PicBadges application (no longer available) is doing this job clearly. Just take a look at their app. Its pretty clear how they have implemented.

They are not directly uploading pictures to "Profile Pictures" album. Instead, they are uploading as usual to their auto generated album (on their app name) and then selecting the pic as "profile pic". However, this method involves redirection of users to page where they need to crop it before getting done.

Interesting implementation to note!

like image 7
Surya Avatar answered Oct 18 '22 19:10

Surya