(对不起我的英文)
我正在使用php和Facebook php sdk 4.0开发a web application。我正在使用FacebookRedirectLoginHelper.php让授权访问他们的信息。我把他们的facebook-id存储在数据库表Users {int UserID FK,nvarchar(max)UserName,nvarchar(max)facebook-id}中。我想列出他们正在使用Web应用程序的facebookfriends。这怎么可能?我不能只改变行:
新的FacebookRequest($ session,'GET','/ me');
到
新的FacebookRequest($ session,'GET','/ me / friends');
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication('XXX','XXX');
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://livescoreapp.azurewebsites.net/' );
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<h1>Welcome</h1>
<?php
if($conn === false)
{
die(print_r(sqlsrv_errors()));
}
else //Connection to database is ok.
{
// see if we have a session
if ( isset( $session ) )
{
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
// print data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
$fb_Id = $graphObject->getProperty('id');
$fb_Name = $graphObject->getProperty('name');
echo "HEY <a href='" . $fb_link . "'>" . $fb_Name . "</a>";
echo "<br> Your fb-id: " . $fb_Id;
// if(CheckIfUserExistsInDatabase($fb_Id, $conn) == false)
// {
// InsertNewUser($conn, $fb_Id, $fb_Name);
// }
}
else {
// show login url
echo '<a href="' . $helper->getLoginUrl() . '">Login</a>';
}
}
?>
此代码打印出来的内容:
!
答案 0 :(得分:2)
要获取用户的朋友,您必须在登录用户时请求user_friends
权限:
$loginUrl = $helper->getLoginUrl( array( 'user_friends' ) );
然后,您将能够按如下方式调用API:
$friends = (new FacebookRequest( $session, 'GET', '/me/friends' ))->execute()->getGraphObject()->asArray();
echo '<pre>' . print_r( $friends, 1 ) . '</pre>';
See my tutorial获得完整的解决方案。
答案 1 :(得分:0)
然后是完整的解决方案:
<?php
// added in v4.0.5
require_once( 'Facebook/FacebookHttpable.php' );
require_once( 'Facebook/FacebookCurl.php' );
require_once( 'Facebook/FacebookCurlHttpClient.php' );
// added in v4.0.0
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookOtherException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphSessionInfo.php' );
// added in v4.0.5
use Facebook\FacebookHttpable;
use Facebook\FacebookCurl;
use Facebook\FacebookCurlHttpClient;
// added in v4.0.0
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;
// start session
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication( 'XXX','YYY' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://livescoreapp.azurewebsites.net/' );
// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );
// validate the access_token to make sure it's still valid
try {
if ( !$session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
// catch any exceptions
$session = null;
}
} else {
// no session exists
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
echo $ex->message;
}
}
// see if we have a session
if ( isset( $session ) ) {
// save the session
$_SESSION['fb_token'] = $session->getToken();
// create a session using saved token or the new one we generated at login
$session = new FacebookSession( $session->getToken() );
// graph api request for user data
$friends = (new FacebookRequest( $session, 'GET', '/me/friends' ))->execute()->getGraphObject()->asArray();
echo '<pre>' . print_r( $friends, 1 ) . '</pre>';
// print logout url using session and redirect_uri (logout.php page should destroy the session)
echo '<a href="' . $helper->getLogoutUrl( $session, 'http://yourwebsite.com/app/logout.php' ) . '">Logout</a>';
} else {
// show login url
echo '<a href="' . $helper->getLoginUrl( array( 'email', 'user_friends' ) ) . '">Login</a>';
}
?>
答案 2 :(得分:0)
请参阅FacebookRedirectLoginHelper.php中的90行....它看起来像
public function getLoginUrl($scope = array(), $version = null)
我改变了
public function getLoginUrl($scope = array('email'), $version = null)
并且工作!!! PD。将所需权限放入&amp; scope数组中。
问候!!!
答案 3 :(得分:0)
$fb = new Facebook\Facebook([/* . . . */]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_likes']; // optional
$loginUrl = $helper->getLoginUrl('http://{your-website}/login-callback.php', $permissions);
echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>';