刚刚玩facebook api但无法获得基本设置。从facebook SDK下载了sdk。我似乎无法使访问令牌工作
$fb = new Facebook\Facebook([
'app_id' => 'keyxx',
'app_secret' => 'key',
'default_graph_version' => 'v2.4',
]);
$helper = $fb->getRedirectLoginHelper();
$accessToken = $helper->getAccessToken();
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?fields=id,name',$accessToken);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$user = $response->getGraphUser();
echo 'Name: ' . $user['name'];
?>
忘记提及我的应用是画布 这是通过重定向的另一种尝试,它不起作用。我只看到一块空白的画布
<?php
require_once 'facebook-php-sdk-v4/src/Facebook/autoload.php';
session_start()
echo 'hello';
$fb = new Facebook\Facebook([
'app_id' => 'x',
'app_secret' => 'y',
'default_graph_version' => 'v2.4',
'default_access_token' => isset($_SESSION['facebook_access_token']) ? $_SESSION['facebook_access_token'] : 'x|y'
]);
try {
$response = $fb->get('/me?fields=id,name');
$user = $response->getGraphUser();
echo 'Name: ' . $user['name'];
exit; //redirect, or do whatever you want
} catch(Facebook\Exceptions\FacebookResponseException $e) {
//echo 'Graph returned an error: ' . $e->getMessage();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
//echo 'Facebook SDK returned an error: ' . $e->getMessage();
}
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_likes'];
$loginUrl = $helper->getLoginUrl('http://www.unsquaringthewheel.com/newTemp/login-callback.php', $permissions);
echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>';
?>
这是回调登录页面
<?php
require_once 'facebook-php-sdk-v4/src/Facebook/autoload.php';
session_start();
$fb = new Facebook\Facebook([
'app_id' => 'x',
'app_secret' => 'y',
'default_graph_version' => 'v2.4',
'default_access_token' => 'x|y'
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
//echo 'Graph returned an error: ' . $e->getMessage();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
//echo 'Facebook SDK returned an error: ' . $e->getMessage();
}
if (isset($accessToken)) {
// Logged in!
$_SESSION['facebook_access_token'] = (string) $accessToken;
} elseif ($helper->getError()) {
// The user denied the request
}
header('Location: index.php');
答案 0 :(得分:-1)
$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.4'
]);
$res = $fb->get('/me', '{access-token}');
<强> Reference 强>
答案 1 :(得分:-1)
对于facebook php sdk4 您可以将访问令牌添加到会话中,如下所示:
$helper = new FacebookRedirectLoginHelper('your redirect url');
$session = $helper->getSessionFromRedirect();
if(isset($session))
{
//error_reporting(0);
// Request for user data
echo $accessToken = $session->getToken();
}
使用getToken()