0

我制作了一个应用程序,可以帮助用户从网站直接将照片发布到 fb 页面。

我现在面临的问题是,如果访问令牌过期,我每次都必须放置新的访问令牌。我需要一种自动获取用户访问令牌的方法。有什么办法吗?这是代码

    <?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
require_once 'library/facebook.php';
$facebook = new Facebook(array(
    'appId'  => 'My_app_ID',
    'secret' => 'My_App_secret',
    'fileUpload' => true
));


#It can be found at https://developers.facebook.com/tools/access_token/
#Change to your token.
$access_token = 'CAAHkfWZB9T84BACm0LqdF1V5BFcppXZCHM7mrRxy9g8SRvw6Wz2RFZBMnYsdgsg413fIE5mpZChgYvz0DiJGGDUsVn8CKkZAFWvm6GHvcGw2xeD8EKH0D7MdkWrdNvD08oXabjzyfPsSTZCxfj1qPg40z4rFUQs8zSOIZD';

$params = array('access_token' => $access_token);

#The id of the fanpage
$fanpage = '232241216922182';

#The id of the album
$album_id ='281594988653471';

//here Wa33D94 is the id of the admin of the page where the photos will be uploaded
$accounts = $facebook->api('/Wa33D94/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');

if(isset($_FILES) && !empty($_FILES)){
    if( !in_array($_FILES['pic']['type'], $valid_files ) ){
        echo 'Only jpg, png and gif image types are supported!';
    }else{
        $img = realpath($_FILES["pic"]["tmp_name"]);

        $args = array(
            'message' => 'This photo was uploaded via clixbox.net',
            '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>';
        }
    }
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Upload pictures to facebook cia Clixbox.net</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style>
html{
    font-family: "lucida grande",tahoma,verdana,arial,sans-serif;
}
.main{
    width:400px;
    margin:auto;
    border:2px solid #0066CC;
    color:#3B5998;
    padding:20px;
    font-size: 11px;
    -moz-border-radius: 4px 4px 4px 4px;
    border-radius: 4px 4px 4px 4px;
    -moz-box-shadow: 1px 1px 0 #d5d5d5;
    background: none repeat scroll 0 0 #F2F2F2;
}
.text{
    color: #777777;
    border: 1px solid #BDC7D8;
    font-size: 11px;
    height: 15px;
}
.post_but {
    background: none repeat scroll 0 0 #EEEEEE;
    border-color: #999999 #999999 #888888;
    border-style: solid;
    border-width: 1px;
    color: #333333;
    cursor: pointer;
    display: inline-block;
    font-size: 11px;
    font-weight: bold;
    padding: 2px 6px;
    text-align: center;
    text-decoration: none;
}
a{
    color:#3B5998;
}
</style>
</head>

<body>

    <div class="main">
        <p>Select a photo to upload on Facebook Fan Page</p>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
        <p>Select the image: <input type="file" name="pic" /></p>
        <p><input class="post_but" type="submit" value="Upload to my album" /></p>
        </form>
    </div>

</body>
</html>
4

1 回答 1

1

facebook对此有一个文档描述。
http://developers.facebook.com/blog/post/2011/05/13/how-to--handle-expired-access-tokens/

于 2013-06-13T12:29:47.310 回答