Search code examples
phpfacebookfacebook-graph-apicurlfacebook-access-token

How to publish an image to a Facebook Page's albums via CURL along with getting new Page Access Tokens?


Currently, I have a website that allows users to upload images and at the same time I would like all these uploaded images to be published automatically to a Facebook page's albums. I used this CURL code below:

    $args = array(
       'message' => $imageDescription,
        'access_token'=>$accesstoken,
        'url' => $img
    );

    $ch = curl_init();
    $url = 'https://graph.facebook.com/' . $albumid . '/photos';
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
    curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);

    $data = curl_exec($ch);

    $response = json_decode($data,true);

From my test, this code works, but only for an hour, because I use the access token generated from the Graph API. This expiration is mentioned in https://developers.facebook.com/docs/pages/access-tokens#expire .

I have been looking around on Stack Overflow, and majority of the questions and answers mentioned the use of app ID and app secret to generate a new token, but this is a page and not an app. There is no app ID and app secret, so I am stuck.

So in this case, what can I do? Or is it not possible to use CURL in this case?


Solution

  • An app in this case doesn't refer to something like a game, it is referring to an API app. This is common for APIs that use OAuth to authenticate users. You create an app for your account and it allows you to access the Facebook API using your user account as the user accessing it.

    The following post explains the entire thing and even has info about how the facebook API works https://stormpath.com/blog/what-the-heck-is-oauth