access_token - Script to upload photo in fan page

2019-07-23 13:56发布

I am developing an app to include in my page tab. In this app, the user will go take a picture with your webcam and the picture will sent to an especific album in my fan page.

I found an script here: and he is working very well, but I've a problem, the script use an user access_token to upload the picture, dont's necessary ask permission of all users to upload the picture because the script use my administrator user to send to the page.

The big problem is: The user access token expire after 2 hours or when my admin user not logged in, the permission offline_access was discontinued and I don't know how my script will work.

I need that all user can upload photo using the system, anyone know how I can work?

Here is the script in PHP:

`
require_once 'library/facebook.php';

$facebook = new Facebook(array(
    'appId'  => '<appid>',
    'secret' => '<appsecret>',
    'fileUpload' => true
));
$access_token = 'access_token';
$params = array('access_token' => $access_token);
$fanpage = 'page_id';
$album_id ='album_id';
$accounts = $facebook->api('/ADMIN_ACCOUNT/accounts', 'GET', $params);

foreach($accounts['data'] as $account) {
    if( $account['id'] == $fanpage || $account['name'] == $fanpage ){
        $fanpage_token = $account['access_token'];
    }
}

$valid_files = array('image/jpeg', 'image/png', 'image/gif');
$img = realpath("image_path");

$args = array(
    'message' => 'message to write in legend',
    'image' => '@' . $img,
    'aid' => $album_id,
    'no_story' => 1,
    'access_token' => $fanpage_token
);

$photo = $facebook->api($album_id . '/photos', 'post', $args);

if( is_array( $photo ) && !empty( $photo['id'] ) ){
    echo '<p><a target="_blank" href="http://www.facebook.com/photo.php?fbid='.$photo['id'].'">Click here to watch this photo on Facebook.</a></p>';
}`

2条回答
Anthone
2楼-- · 2019-07-23 14:21

You will need to request a long-lived access token by hitting the endpoint:

https://graph.facebook.com/oauth/access_token?             
    client_id=APP_ID&
    client_secret=APP_SECRET&
    grant_type=fb_exchange_token&
    fb_exchange_token=EXISTING_ACCESS_TOKEN 

Take a look at Scenario 4 on the following document: http://developers.facebook.com/roadmap/offline-access-removal/

查看更多
可以哭但决不认输i
3楼-- · 2019-07-23 14:34

If you’re using that script to post to a fan page’s album, then you should get a page access token – they don’t expire.

Details see here: https://developers.facebook.com/docs/authentication/pages/

查看更多
登录 后发表回答