Facebook Graph用户图片

时间:2014-10-30 09:19:03

标签: php facebook image facebook-graph-api

我在使用PHP登录系统时使用的是facebook,但是它不会从Facebook个人资料中获取上传的图片,只能获取个人资料图片以及来自Facebook的正常内容,如电子邮件和用户ID

<?php

/* INCLUSION OF LIBRARY FILEs*/
require_once( 'lib/Facebook/FacebookSession.php');
require_once( 'lib/Facebook/FacebookRequest.php' );
require_once( 'lib/Facebook/FacebookResponse.php' );
require_once( 'lib/Facebook/FacebookSDKException.php' );
require_once( 'lib/Facebook/FacebookRequestException.php' );
require_once( 'lib/Facebook/FacebookRedirectLoginHelper.php');
require_once( 'lib/Facebook/FacebookAuthorizationException.php' );
require_once( 'lib/Facebook/GraphObject.php' );
require_once( 'lib/Facebook/GraphUser.php' );
require_once( 'lib/Facebook/GraphSessionInfo.php' );
require_once( 'lib/Facebook/Entities/AccessToken.php');
require_once( 'lib/Facebook/HttpClients/FacebookCurl.php' );
require_once( 'lib/Facebook/HttpClients/FacebookHttpable.php');
require_once( 'lib/Facebook/HttpClients/FacebookCurlHttpClient.php');

/* USE NAMESPACES */

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphUser;
use Facebook\GraphSessionInfo;
use Facebook\FacebookHttpable;
use Facebook\FacebookCurlHttpClient;
use Facebook\FacebookCurl;

/*PROCESS*/

//Stat Session
 session_start();

//check if users wants to logout
 if(isset($_REQUEST['logout'])){
    unset($_SESSION['fb_token']);
 }

//Use app id,secret and redirect url 
$app_id = '****';
$app_secret = '*****';
$redirect_url='http://www.url.com/index.php';

//Initialize application, create helper object and get fb sess
 FacebookSession::setDefaultApplication($app_id,$app_secret);
 $helper = new FacebookRedirectLoginHelper($redirect_url);
 $sess = $helper->getSessionFromRedirect();

 //check if facebook session exists
if(isset($_SESSION['fb_token'])){
    $sess = new FacebookSession($_SESSION['fb_token']);
    try{
        $sess->Validate($id, $secret);
    }catch(FacebookAuthorizationException $e){
        print_r($e);
    }
}

$loggedin = false;
//get email as well with user permission
$login_url = $helper->getLoginUrl(array('email, user_birthday, user_likes, user_friends, user_website, user_photos'));
//logout
$logout = 'http://url.com/index.php';

//if fb sess exists echo name 
    if(isset($sess)){
        //store the token in the php session
        $_SESSION['fb_token']=$sess->getToken();
        //create request object,execute and capture response
        $request = new FacebookRequest($sess,'GET','/me');
        // from response get graph object
        $response = $request->execute();
        $graph = $response->getGraphObject(GraphUser::classname());
        // use graph object methods to get user details
        $id = $graph->getId();
        $firstname = $graph->getFirstName();
        $lastname = $graph->getLastName();
        $name = $graph->getName();
        $email = $graph->getProperty('email');
        $image = 'https://graph.facebook.com/'.$id.'/picture?width=64&height=64';
        $userphoto = 'https://graph.facebook.com/'.$id.'/photos/uploaded';
        $loggedin  = true;

$userphoto变量是需要显示来自用户的上传照片的变量,但此变量不会显示我的任何照片。我做错了什么?

2 个答案:

答案 0 :(得分:0)

您需要设置权限&#34; user_photos&#34;在api中,否则您无法访问该帐户的任何图片。

答案 1 :(得分:0)

如果所有这些附加权限都不起作用,您很可能不会尝试使用App开发人员,并且Facebook不会批准这些权限。大多数权限需要先获得批准才能使用 - 否则它们只适用于在App中有角色的人(Admin / Developer / Tester):

如果这不是问题,请尝试通过将访问令牌放入调试器来检查权限是否设置正确:https://developers.facebook.com/tools/debug/ - 如果权限没有显示,则表示存在问题登录过程。

还有一个想法,你可能没有正确使用getLoginURL:

$params = array(
  'scope' => 'read_stream, friends_likes',
  'redirect_uri' => 'https://www.myapp.com/post_login_page'
);

$loginUrl = $facebook->getLoginUrl($params);

来源:https://developers.facebook.com/docs/reference/php/facebook-getLoginUrl/

顺便说一句,我建议使用附带的自动加载器,而不是所有那些无数的require语句。