使用Facebook SDK获取Facebook页面列表以及页面ID和访问令牌
我已经创建了一个简单的仪表板,在那里我可以登录Facebook。但是我找不到页面列表。我已启用这些权限使用Facebook SDK获取Facebook页面列表以及页面ID和访问令牌,facebook,facebook-php-sdk,Facebook,Facebook Php Sdk,我已经创建了一个简单的仪表板,在那里我可以登录Facebook。但是我找不到页面列表。我已启用这些权限管理页面页面管理帖子页面显示列表 我想获得我授权的页面的页面ID和访问令牌,然后将其存储在我的数据库中。这样我以后就可以在不打开Facebook的情况下发布这些页面 我的使用案例:使用Facebook登录,授权页面,获取页面ID和访问令牌,将其存储到我的数据库,然后使用这些凭据创建帖子 这些是我创建的代码 config.php <?php session_start(); require_
管理页面
页面管理帖子
页面显示列表
我想获得我授权的页面的页面ID和访问令牌,然后将其存储在我的数据库中。这样我以后就可以在不打开Facebook的情况下发布这些页面
我的使用案例:使用Facebook登录,授权页面,获取页面ID和访问令牌,将其存储到我的数据库,然后使用这些凭据创建帖子
这些是我创建的代码
config.php
<?php
session_start();
require_once('Facebook/autoload.php');
$FBObject = new \Facebook\Facebook([
'app_id' => 'HIDDEN',
'app_secret' => 'HIDDEN',
'default_graph_version' => 'v2.10'
]);
$handler = $FBObject -> getRedirectLoginHelper();
?>
<?php
require_once('config.php');
if(isset($_SESSION['access_token'])){
header("Location: index.php");
exit();
}
$redirectTo = "http://localhost/callback.php";
$data = ['email'];
$fullURL = $handler->getLoginUrl($redirectTo, $data);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<title>Login with Facebook</title>
</head>
<body>
<div class="container" style="margin-top: 100px">
<div class="row justify-content-center">
<div class="col-md-6 col-md-offset-3" align="center">
<form>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">Check me out</label>
</div>
<input type="submit" value="Enter" class="btn btn-primary">
<input type="button" onclick="window.location = '<?php echo $fullURL ?>'" value="Login with Facebook" class="btn btn-primary">
</form>
</div>
</div>
</div>
</body>
</html>
<?php
require_once("config.php");
try {
$accessToken = $handler->getAccessToken();
}catch(\Facebook\Exceptions\FacebookResponseException $e){
echo "Response Exception: " . $e->getMessage();
exit();
}catch(\Facebook\Exceptions\FacebookSDKException $e){
echo "SDK Exception: " . $e->getMessage();
exit();
}
if(!$accessToken){
header('Location: login.php');
exit();
}
$oAuth2Client = $FBObject->getOAuth2Client();
if(!$accessToken->isLongLived())
$accessToken = $oAuth2Client->getLongLivedAccesToken($accessToken);
$response = $FBObject->get("/me?fields=id, first_name, last_name, email, picture.type(large)", $accessToken);
$userData = $response->getGraphNode()->asArray();
$_SESSION['userData'] = $userData;
$_SESSION['access_token'] = (string) $accessToken;
$requestxx = new FacebookRequest(
$fbApp,
$_SESSION['access_token'],//my user access token
'GET',
'/{page-id}?fields=access_token',
array( 'ADMINISTER' )
);
$responset = $fb->getClient()->sendRequest( $requestxx );
$json = json_decode( $responset->getBody() );
$page_access = $json->access_token;
header('Location: index.php');
exit();
?>
<?php
session_start();
if(!isset($_SESSION['access_token'])){
header("Location: login.php");
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<title>My profile</title>
</head>
<body>
<div class="container" style="margin-top: 100px">
<div class="row justify-content-center">
<div class="col-md-3">
<img src="<?php echo $_SESSION['userData']['picture']['url'] ?>">
</div>
<div class="col-md-9">
<table class="table table-hover table-bordered">
<tbody>
<tr>
<td>ID</td>
<td><?php echo $_SESSION['userData']['id'] ?></td>
</tr>
<tr>
<td>First Name</td>
<td><?php echo $_SESSION['userData']['first_name'] ?></td>
</tr>
<tr>
<td>Last Name</td>
<td><?php echo $_SESSION['userData']['last_name'] ?></td>
</tr>
<tr>
<td>Email</td>
<td><?php echo $_SESSION['userData']['email'] ?></td>
</tr>
<tr>
<td>Token</td>
<td><?php echo $_SESSION['access_token'];?></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
login.php
<?php
session_start();
require_once('Facebook/autoload.php');
$FBObject = new \Facebook\Facebook([
'app_id' => 'HIDDEN',
'app_secret' => 'HIDDEN',
'default_graph_version' => 'v2.10'
]);
$handler = $FBObject -> getRedirectLoginHelper();
?>
<?php
require_once('config.php');
if(isset($_SESSION['access_token'])){
header("Location: index.php");
exit();
}
$redirectTo = "http://localhost/callback.php";
$data = ['email'];
$fullURL = $handler->getLoginUrl($redirectTo, $data);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<title>Login with Facebook</title>
</head>
<body>
<div class="container" style="margin-top: 100px">
<div class="row justify-content-center">
<div class="col-md-6 col-md-offset-3" align="center">
<form>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">Check me out</label>
</div>
<input type="submit" value="Enter" class="btn btn-primary">
<input type="button" onclick="window.location = '<?php echo $fullURL ?>'" value="Login with Facebook" class="btn btn-primary">
</form>
</div>
</div>
</div>
</body>
</html>
<?php
require_once("config.php");
try {
$accessToken = $handler->getAccessToken();
}catch(\Facebook\Exceptions\FacebookResponseException $e){
echo "Response Exception: " . $e->getMessage();
exit();
}catch(\Facebook\Exceptions\FacebookSDKException $e){
echo "SDK Exception: " . $e->getMessage();
exit();
}
if(!$accessToken){
header('Location: login.php');
exit();
}
$oAuth2Client = $FBObject->getOAuth2Client();
if(!$accessToken->isLongLived())
$accessToken = $oAuth2Client->getLongLivedAccesToken($accessToken);
$response = $FBObject->get("/me?fields=id, first_name, last_name, email, picture.type(large)", $accessToken);
$userData = $response->getGraphNode()->asArray();
$_SESSION['userData'] = $userData;
$_SESSION['access_token'] = (string) $accessToken;
$requestxx = new FacebookRequest(
$fbApp,
$_SESSION['access_token'],//my user access token
'GET',
'/{page-id}?fields=access_token',
array( 'ADMINISTER' )
);
$responset = $fb->getClient()->sendRequest( $requestxx );
$json = json_decode( $responset->getBody() );
$page_access = $json->access_token;
header('Location: index.php');
exit();
?>
<?php
session_start();
if(!isset($_SESSION['access_token'])){
header("Location: login.php");
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<title>My profile</title>
</head>
<body>
<div class="container" style="margin-top: 100px">
<div class="row justify-content-center">
<div class="col-md-3">
<img src="<?php echo $_SESSION['userData']['picture']['url'] ?>">
</div>
<div class="col-md-9">
<table class="table table-hover table-bordered">
<tbody>
<tr>
<td>ID</td>
<td><?php echo $_SESSION['userData']['id'] ?></td>
</tr>
<tr>
<td>First Name</td>
<td><?php echo $_SESSION['userData']['first_name'] ?></td>
</tr>
<tr>
<td>Last Name</td>
<td><?php echo $_SESSION['userData']['last_name'] ?></td>
</tr>
<tr>
<td>Email</td>
<td><?php echo $_SESSION['userData']['email'] ?></td>
</tr>
<tr>
<td>Token</td>
<td><?php echo $_SESSION['access_token'];?></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
使用Facebook登录
电子邮件地址
我们永远不会与其他人共享您的电子邮件。
密码
看看我
你说你“启用”了那些权限是什么意思?您在代码中唯一需要的是电子邮件
–$data=['email']代码>