access_token - Script to upload photo in fan page album

我怕爱的太早我们不能终老 提交于 2019-12-10 23:17:41

问题


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>';
}`

回答1:


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/




回答2:


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/



来源:https://stackoverflow.com/questions/10918954/access-token-script-to-upload-photo-in-fan-page-album

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!