0

I have developed an app so that users can upload images to a page's album. I do not want users to have to authorize and so I used the access token from the app to upload the images for them. This works fine if the admin is logged in and the access token hasn't expired - it only seems to last for a couple hours. I have looked through stackoverflow and facebook and tried many different things and nothing seems to work. Here is some of the code I am using:

try {
        $facebook->setAccessToken("APP_ACCESS_TOKEN_FROM_GRAPH_API_TOOL");
        $token_url="https://graph.facebook.com/oauth/access_token?client_id=APP_ID&client_secret=APP_SECRET&grant_type=fb_exchange_token&fb_exchange_token=".$facebook->getAccessToken();
        $page_id = 'PAGE_ID';
        $response = file_get_contents($token_url);
        $params = null;
        parse_str($response, $params);
        $page_info['access_token'] = $params['access_token'];
        $page_info = $facebook->api("/".$page_id."?fields=access_token");
        if( !empty($page_info['access_token']) ) {
            $photo_details['access_token'] = $page_info['access_token'];
            $upload_photo = $facebook->api('/'.$page_id.'/photos', 'post', $photo_details);

Am I dreaming? Is this possible and I just screwed up the code? Any help would be much appreciated even just to point me in the right direction...

4
  • How are you logging in the page admin? Are you using the client side or server side Oauth flow? When you use the server side flow for the admin to get a 60-day token for them, then grab the page access token using the /accounts endpoint, the page access token should be infinite
    – Igy
    Dec 20, 2012 at 23:14
  • I am using the php-sdk so I would use the server side... Currently there is no oauth flow other than the first 2 lines that you see after try{. The setAccessToken takes the access_token from the admin/app and uses it in the graph api call on the line right after. Dec 20, 2012 at 23:20
  • If you're using the server side flow, the token you get for the page admin should be 60 day, and the page access token you retrieve from /<user>/accounts should be then infinite - if this isn't working, try uninstalling the app forcibly (/me/permissions?method=delete&access_token=XXX) and reinstalling and see if it works then
    – Igy
    Dec 20, 2012 at 23:25
  • Thanks Igy! I found on this [link][1] how to make the APP_ACCESS_TOKEN_FROM_GRAPH_API_TOOL into a 60 day access token and now the admin doesn't need to be logged in and the access tokens don't expire. Crossing my fingers that it continues to work this way! [1]: stackoverflow.com/questions/12168452/… Dec 20, 2012 at 23:47

1 Answer 1

0

Currently, your code uses your own Access Token, not the App Access token.

To get the app access token, you need to make a get request to https://graph.facebook.com/oauth/access_token?client_id=APP_ID&client_secret=APP_SECRET&grant_type=client_credentials as documented at https://developers.facebook.com/docs/technical-guides/opengraph/publishing-with-app-token/

in your implementation, and assuming that APP_SECRET and APP_ID are defined as constants, you'd get the app access token through :

$appAccessToken = str_replace("access_token=","",$facebook->api("https://graph.facebook.com/oauth/access_token?client_id=APP_ID&client_secret=APP_SECRET&grant_type=client_credentials"));
$facebook->setAccessToken($appAccessToken);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.