Search code examples
facebook-graph-api

Create profile photo for user on users facebook page


I'm struggling creating a profile picture on behalf of a user on facebook.

What endpoint do I need to use to update the profile image for a user on facebook? And what permissions do i need as well?

I have the access token and I'm using react native.


Solution

  • Scopes

    1. if querying an App-Scoped User ID:
    • None
    1. If querying a User ID:
    • User or Page access token for Facebook Login authenticated requests
    • App access token for server-side requests
    • Client access token for mobile or web client-side requests
    1. If querying a Page-Scoped User ID:
    • Page

    Endpoint

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