27
votes

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.

5

5 Answers

43
votes

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>";
}
7
votes

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!

5
votes

You can upload to the user's Profile Picture album using the Graph API but it appears that you cannot update the /me/picture value to set the users current profile image to the image you've uploaded.

0
votes

User Picture: Graph API Version v6.0

Reading

You can get the pictue with the endpoint /{user-id}/picture

Creating

You can't perform this operation on this endpoint.

Updating

You can't perform this operation on this endpoint.

Deleting

You can't perform this operation on this endpoint.

0
votes

I had this problem too. I've managed to upload a profile picture using this endpoint.

This link provides information about how to upload a profile picture

        private async uploadProfilePhoto(pageId:string, accessToken: string, photoUrl: string){
            let url = FACEBOOK_API_URL + `${pageId}/picture`+ 
                `?access_token=${accessToken}`+
                `&picture=${photoUrl}`;
            let response = null;

            try {
             response = await axios({
                method: 'post',
                url:  url,
            }); 
            } catch (err) {
                /**  Here this error occures but the profile image is still uploaded. 
                 * {message: 'Unsupported post request.', type: 'GraphMethodException', code: 100, fbtrace_id: 'AGhsadasdaiqyf_YHJaztdasdadG7'
                 */           
            }

            return response;
        }

Then to get the id of the profile picture in case you need it you can perform a get request on this endpoint.

const FACEBOOK_API_URL ='https://graph.facebook.com/v7.0/'
let url = FACEBOOK_API_URL + `${pageId}/photos`+
            `?access_token=${accessToken}` +
            `&fields=picture`;