28

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.

Community
  • 1
  • 1
Mauro Zadunaisky
  • 798
  • 1
  • 11
  • 21
  • possible duplicate of [Can I set a users profile image using the Facebook API?](http://stackoverflow.com/questions/1648859/can-i-set-a-users-profile-image-using-the-facebook-api) – alpere Mar 06 '15 at 16:06

5 Answers5

43

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>";
}
Owen Blacker
  • 4,117
  • 2
  • 33
  • 70
fredrik
  • 13,282
  • 4
  • 35
  • 52
  • 1
    Thanks. Facebook doesn´t mind us doing this? No policy violation? – Mattias Mar 16 '12 at 07:30
  • No, not that I know of. I've used it in a live app, and there where no problems there. – fredrik Mar 16 '12 at 08:42
  • 6
    @Mattias I don't expect this to be a violation of policy, because you're not setting the profile image yourself. It only pops up a dialog in which the user is allowed to do so and he can choose not to. – aalaap May 06 '12 at 08:12
  • 1
    this isn't working. I am trying using graph api explorer with url https://graph.facebook.com/albumid/photos Fields :- message, url, makeprofile What is it am i doing wrong ? – Sanket Aug 24 '12 at 14:54
  • 1
    How to get album_uid – salih kallai Apr 30 '16 at 11:18
  • Help, I don't achieve to get the picture link – Sebastián Rojas May 20 '16 at 05:24
  • @fredrik, salih kallai has a good question: how do you get the `album_id`? – Alexis Wilke Aug 06 '16 at 19:00
  • 1
    @AlexisWilke I haven't used the facebook apis in many years, but a get on /me/albums seems to get you the ids of the albums (https://developers.facebook.com/tools/explorer?method=GET&path=me%2Falbums&version=v2.7) – fredrik Aug 07 '16 at 21:41
  • @fredrik, indeed, I got them that way and you can then query that album (`/`) for its type and know whether it is the user profile pictures or not. – Alexis Wilke Aug 07 '16 at 22:53
  • 3
    Sadly the 'makeprofile=1' trick doesn't seem to work anymore. It *is* the URL that facebook uses, but it only works if you click on it from the theater view. – David Ljung Madison Stellar Nov 15 '16 at 01:20
  • Considering that this method requires eventual user confirmation, isn't https://stackoverflow.com/a/6309984/9731176 a more concise replacement for the last section? – rokejulianlockhart Jun 07 '23 at 13:14
  • @DavidLjungMadisonStellar, that probably just means that someone needs to see what the website sends OTA using a packet sniffer, right? I can't imagine it's anything inherently server-side. – rokejulianlockhart Jun 07 '23 at 13:16
  • What language is this? – rokejulianlockhart Jun 07 '23 at 16:21
8

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!

Per Enström
  • 902
  • 8
  • 33
Surya
  • 4,824
  • 6
  • 38
  • 63
5

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.

Patrick Gidich
  • 1,127
  • 1
  • 8
  • 12
0

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

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`;
Alessio
  • 3,404
  • 19
  • 35
  • 48
Milo
  • 61
  • 6