0

我正在创建一个网站,使用他们的 Facebook 帐户登录的用户将在我的网站上访问他们的相册和图片。我必须在我的网站中输入什么代码才能做到这一点。我能够获得所有登录用户的所有一般信息,但不能获得他们的信息和图片。只有当我使用我的 Facebook id 登录时,所有代码才能正常工作,然后它也会提供我所有的信息图片和相册。我希望这个东西适用于所有用户。我已经阅读了很多来自 Facebook 和堆栈溢出的文档,但我找不到答案,这是我的演示链接

这是我的代码:

    require '../php-sdk/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => 'xxxxxxxxxxx',
  'secret' => 'xxxxxxxxxxxxxxxxxxxxxx',

));

// Get User ID
$user = $facebook->getUser();
$user_id = $user; 
$access_token = $facebook->getAccessToken();


$SESSION['token']="CAAHk9LIIji4BAMLZCNFVMDSNEZCZCc5zfb80c8Nq4wrLZBqY56o3ZARrO3FZC9qPWXHdYpH5jZBaaADWsZBKZAdViKA5eQlKcFK579SOlOq2CXms8ZAqGZAJdXsUZAH2ZCtqPZASn3A8ZB7VXFxb4WFjPJTPUNDQlZBZCtqHcJNZA3TRozriLDCjjRdsPp5Bad9Mu1J9CdGJQZD";
 $SESSION['token']=  $access_token;
echo $url = "https://graph.facebook.com/me/photos?access_token=" . $SESSION['token'];
$photos = json_decode(file_get_contents($url));

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/'.$user_id);
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $statusUrl = $facebook->getLoginStatusUrl();
  $loginUrl = $facebook->getLoginUrl();
}


?>

 <?php if ($user): ?>

  <a href="<?php echo $logoutUrl; ?>">Logout</a>
<?php else: ?>
  <div>
    Check the login status using OAuth 2.0 handled by the PHP SDK:
    <a href="<?php echo $statusUrl; ?>">Check the login status</a>
  </div>
  <div>
    Login using OAuth 2.0 handled by the PHP SDK:
    <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
  </div>
<?php endif ?>

<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>

<?php if ($user): ?>  

  <h3>You</h3>
  <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
  <pre><?php print_r($user_albums); ?></pre>
  <h3>Your User Object (/me)</h3>
  <pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
  <strong><em>You are not Connected.</em></strong>
<?php endif ?>

<h3>Public profile of Naitik</h3>
<img src="https://graph.facebook.com/naitik/picture">
<?php echo $naitik['name']; ?>


if ($user) {
       echo "album";
    // We have a user ID, so probably a logged in user.
    // If not, we'll get an exception, which we handle below.
    try {

        $params = array(
            'method' => 'get',
            'access_token' => $access_token
        );

        if (isset($_GET['aid']) && $_GET['aid'] != '') {
            $aid = $_GET['aid'];
            $user_album_photos = $facebook->api('/' . $aid . '/photos', $params);
            //echo "Photos<br/>"; 
            ?>

            <?php foreach ($user_album_photos['data'] as $key => $value) {
                ?>
        <div class="album">
         <div class="frame photo_frame">
        <div class="edit-photo-nohover" style="display:block">
        <div><input type="checkbox" id="fbimport_id<?php echo $value['id']; ?>" value="<?= $value['id'] . ',' . $value['images']['0']['source'] . ',' . $value['name'] ?>" name="fbimport[]" > <span>Import this Memory</span></div>

                        </div>
                        <table class="test">
                            <tr><td>
                                    <a href="javascript:void(0)"><img src="<?= $value['images']['0']['source'] ?>" height="100" width="100" /></a>
                                </td>
                            </tr>
                        </table>
                        <h3 id='bottomcaption'><?php echo $value['name']; ?></h3>
                    </div><br/>
                </div>
                <?php }
            ?>


        <?php
        } else {
            $user_albums = $facebook->api('/'.$user_id.'/albums', $params);
            echo '<h3 class="page-title">Select Your Facebook Album</h3><br/><br/>';
            foreach ($user_albums['data'] as $key => $value) {

                /* load album if not blank */
                if (isset($value['count']) && $value['count'] != '' && $value['count'] != NULL && $value['count'] > 0) {

                    /* check if album has a cover photo. if not than load a default image */
                    if (isset($value['cover_photo']) && $value['cover_photo'] != '' && $value['cover_photo'] != NULL) {
                        $user_album_cover = $facebook->api('/' . $value['cover_photo'], $params);
                        $album_thumbnail = $user_album_cover['images']['0']['source'];
                    } else {
                        $album_thumbnail = 'default_thumb.gif';
                    }
                    /* check if album has cover photo end */
                    ?>
                    <div class="album">
                        <div class="frame photo_frame">
                            <table class="test">
                                <tr><td>
                                        <a href="?aid=<?= $value['id'] ?>" ><img src="<?= $album_thumbnail ?>" height="100" width="100" /></a>
                                    </td>
                                </tr>
                            </table>
                            <h3 id='bottomcaption'><?php echo $value['name']; ?></h3>
                        </div><br/>
                    </div>

                    <?php
                }//if(isset($value['count']) && $value['count'] != '' && $value['count'] != NULL && $value['count']>0)
                /* load album if not blank end */
            }
        }
    } catch (FacebookApiException $e) {
        // If the user is logged out, you can have a 
        // user ID even though the access token is invalid.
        // In this case, we'll get an exception, so we'll
        // just ask the user to login again here.
        $login_url = $facebook->getLoginUrl();
        echo 'Please <a href="' . $login_url . '">login.</a>';
        error_log($e->getType());
        error_log($e->getMessage());
    }
} else {

    // No user, print a link for the user to login
    $login_url = $facebook->getLoginUrl();
    echo 'Please <a href="' . $login_url . '">login.</a>';
}
?>
4

1 回答 1

2

我认为您需要征得用户的许可才能使用他们的图片。您需要将一组权限传递给获取登录 url 的函数。您需要的权限是“user_photos”。

有关更多信息,请参阅这些链接:

权限

getLoginUrl 的文档

于 2013-10-27T11:42:05.980 回答